JSP and JSTL Tutorials - Tutorial Addendum - Tags Alive Calm

android appcolt wargamesios appcolt
 31 December 18:00   

    



    



    

(Continued from antecedent part...)

    



    

I am abiding you can apprehend this page, and accept what I am accomplishing with the loop.

    

Warning, my "break" tag is not a absolutely breach statement. If there is any additional

    

body agreeable in the bend tag afterwards the "break" tag, it will not be skipped.

    



    

Sharing Data with Additional Tags

    



    

In the antecedent example, we looked at how tags can be nested central anniversary other,

    

and how adolescent tags can admission ancestor tags to adapt their behavior.

    

In the next example, we will attending at how non-nested tags, brother tags, can

    

communicate to anniversary other.

    



    

If a tag wants to allotment data with a brother tag, it haveto abundance the data to a common

    

place area both of them accept access. Obviously, that accepted abode is the pageContext

    

object.

    

The JSP tag addendum ability offers to every tag item the admission to pageContext

    

object by the setPageContext() alarm in Tag interface. If you use the TagSupport

    

implementation class, pageContext is already create accessible as an instance variable.

    



    

If you apprehend the PageContext chic API, you will see that it allows you to

    

store and retrieve altar as called attributes at any time . So if one tag wants

    

to allotment an item to addition tag, it can abundance that item to pageContext;

    

and the additional tag can retrieve it from pageContext.

    



    

Another advantage of using pageContext to allotment altar is that JSTL tags

    

are aswell using pageContext to abundance and allotment objects. So if we use it correctly,

    

we can allotment altar in custom tags with JSTL tags.

    



    

Let s attending at a actual simple example, SetTimeTag.java. It does annihilation but storing

    

the accepted time in milliseconds into pageContext as an aspect with a accustomed name.

    

Once the data is stored in pageContext, any additional tags can appear and retrieve it.

    

 

    

/**

    

* SetTimeTag.java

    

* Absorb (c) 2003 by Dr. Yang. All rights reserved.

    

*/

    

package ;

    

import javax.servlet.jsp.tagext.*;

    

public chic SetTimeTag extends TagSupport {

    

clandestine Cord var = null;

    

accessible abandoned setVar(String v) {

    

this.var = v;

    

}

    

accessible int doStartTag() {

    

Continued now = new Long(System.currentTimeMillis());

    

pageContext.setAttribute(var,now);

    

acknowledgment SKIP_BODY;

    

}

    

}

    



    



    

The TLD file:

    

 

    

<?xml version="1.0"?>

    

<!DOCTYPE taglib Accessible

    

"-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"

    

"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_2.dtd">

    

<!-- HyTaglib.tld

    

Absorb (c) 2003 by Dr. Yang

    

-->

    

<taglib>

    

<tlib-version>1</tlib-version>

    

<jsp-version>1.2</jsp-version>

    

<short-name> s Tag Library</short-name>

    

<tag>

    

<name>setTime</name>

    

<tag-class>.SetTimeTag</tag-class>

    

<body-content>empty</body-content>

    

<attribute>

    

<name>var</name>

    

<required>true</required>

    

</attribute>

    

</tag>

    

<!-- additional tags -->

    

</taglib>

    



    



    

Here is the testing page:

    

 

    

<?xml version="1.0"?>

    

<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"

    

xmlns:c="http://java.sun.com/jstl/core"

    

xmlns:hy="urn:jsptld:/WEB-INF/tlds/HyTaglib.tld" version="1.2">

    

<!-- SetTimeTagTest.jsp

    

Absorb (c) 2003 by Dr. Yang

    

-->

    

<jsp:directive.page contentType="text/html"/>

    

<html><body>

    

<hy:setTime var="t1"/>

    

<p>Checking prime numbers:</p>

    

<c:set var="upperLimit" value="${50}"/>

    

<c:forEach var="i" begin="${3}" end="${upperLimit}">

    

<c:set var="isPrime" value="${true}"/>

    

<c:forEach var="j" begin="${2}" end="${i-1}">

    

<c:if test="${i%j == 0}">

    

<c:set var="isPrime" value="${false}"/>

    

<!-- We should breach the bend actuality -->

    

</c:if>

    

</c:forEach>

    

<c:choose>

    

<c:when test="${isPrime}">

    

<c:out value="${i} is a prime number."/><br/>

    

</c:when>

    

<c:otherwise>

    

<c:out value="${i} is a not prime number."/><br/>

    

</c:otherwise>

    

</c:choose>

    

</c:forEach>

    

<hy:setTime var="t2"/>

    

<p><c:out value="Total time = ${t2-t1} milliseconds."/></p>

    

</body></html>

    

</jsp:root>

    



    



    

The output:

    

 

    

Checking prime numbers:

    

3 is a prime number.

    

4 is a not prime number.

    

5 is a prime number.

    

6 is a not prime number.

    

7 is a prime number.

    

8 is a not prime number.

    

9 is a not prime number.

    

10 is a not prime number.

    

...

    

50 is a not prime number.

    

Total time = 110 milliseconds.

    



    



    

As you can see from the testing page, I acclimated "setTime" to abundance the accepted time

    

at the alpha of my prime amount blockage action as "t1", and the current

    

time at the end as "t2". Then I acclimated "out" to retrieve them and account their

    

difference in a individual expression.

    



    



    

 


Tags: access, example, numbers, class, content, store, together, value, prime, attribute, notes, public, tutorial, tutorials, retrieve, working, object

 prime, pagecontext, version, value, class, store, share, public, objects, retrieve, foreach, taglib, xmlns, settime, checking, milliseconds, access, example, object, settimetag, attribute, content, copyright, , prime number, tags can, < jsp, < body, html <, share objects, < tag, name <, sun com, http java, java sun, checking prime numbers, var isprime value, tags working together, notes tags working, tutorials tutorial notes, tutorial notes tags, jstl tutorials tutorial,

Share JSP and JSTL Tutorials - Tutorial Addendum - Tags Alive Calm:
Digg it!   Google Bookmarks   Del.icio.us   Yahoo! MyWeb   Furl  Binklist   Reddit!   Stumble Upon   Technorati   Windows Live   Bookmark

Text link code :
Hyper link code:

Also see ...

Permalink
Article In : Computers & Technology  -  jsp