SlideShare une entreprise Scribd logo
1  sur  49
Chapter 8.
TAG LIBRARIES:
Advanced Features
Topics in This Chapter
 Manipulating tag body
 Assigning dynamic values to tag attributes
 Assigning complex objects as values to tag attributes
 Creating looping tags
 Creating expression language functions
 Working with nested custom tags
8.1 Manipulating Tag Body
 In Chapter 7 we have seen how to include the body of the tag in
the output of the Java-based custom tag.
 To review, there are essentially two things you need to do.
 Specify scriptless in the body-content element of the TLD for the tag.
 This allows the page author to insert JSP content between the opening and
closing elements of the tag.
 Remember that this JSPcontent is not allowed to have any scripting
elements like <% ... %> or <%= ... %>.
 Invoke the output of the tag body by calling getJspBody().invoke(null)
inside the doTag method of the tag handler class.
 Remember that this statement passes the output of the JSP content to the
client, not the actual JSP code itself.
8.1 Manipulating Tag Body
 The invoke method takes a Writer as its argument.
 If null is passed to the invoke method, the container
directs the output of the tag body to the JspWriter
object.
 The server obtains a reference to this object by calling
methods similar to getJspContext().getOut(). In other
words, the statements getJspBody().invoke(null); and
getJspBody().invoke(getJspContext().getOut());
accomplish exactly the same results.
8.1 Manipulating Tag Body
 Pass a different Writer to the invoke method.
 Using the new Writer, we can buffer up the output of the JSP content, extract it from
the Writer, manipulate it, and output the new content to the client.
 The following are the steps of how this can be accomplished.
 Create an instance of a convenient Writer. Any class that inherits from
java.io.Writer class is acceptable. Because the output to the client is usually
HTML, which is just a string, the java.io.String- Writer class is the most
common Writer to use.
 For example: StringWriter myWriter = new StringWriter();
 Pass the newly created StringWriter as an argument to the invoke method.
 For example: getJspBody().invoke(myWriter);
 Note that now the output of the tag body is not sent to the client but
buffered up inside myWriter.
 Extract the buffered output from the Writer, modify it, and send the
 modified version to the client like so:
 String modified = modify(myWriter.toString());
 getJspContext().getOut().print(modified);
8.2 Example: HTML-Filtering Tag
 In this example, we create a custom tag, filterhtml,
that filters out the HTML code, converting it to
regular text, thus preventing the browser from
interpreting it as HTML code. We are now able to see
the actual unrendered HTML code .
 We define 3 components as follows,
 Tag Handler Class (HtmlFilterTag)
 TLD file (csajsp-taglib-adv.tld)
 JSP page (html-filter.jsp)
8.2 Example: HTML-Filtering Tag
 Tag Handler Class : HtmlFilterTag.java
package coreservlets.tags;
import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.io.*;
import coreservlets.ServletUtilities;
public class HtmlFilterTag extends SimpleTagSupport
{
public void doTag() throws JspException, IOException
{
StringWriter stringWriter = new StringWriter();
getJspBody().invoke(stringWriter);
String output =ServletUtilities.filter(stringWriter.toString());
JspWriter out = getJspContext().getOut();
out.print(output);
}
}
8.2 Example: HTML-Filtering Tag
 ServletUtilities.java
package coreservlets;
import javax.servlet.*;import javax.servlet.http.*;
public class ServletUtilities {
public static String filter(String input) {
if (!hasSpecialChars(input))
{ return(input); }
StringBuffer filtered = new StringBuffer(input.length());
char c;
for(int i=0; i<input.length(); i++)
{
c = input.charAt(i);
switch(c) {
case '<': filtered.append("&lt;"); break;
case '>': filtered.append("&gt;"); break;
case '"': filtered.append("&quot;"); break;
case '&': filtered.append("&amp;"); break;
default: filtered.append(c);
}
}
return(filtered.toString());
}
8.2 Example: HTML-Filtering Tag
private static boolean hasSpecialChars(String input)
{
boolean flag = false;
if ((input != null) && (input.length() > 0))
{ char c;
for(int i=0; i<input.length(); i++)
{
c = input.charAt(i);
switch(c)
{
case '<': flag = true; break;
case '>': flag = true; break;
case '"': flag = true; break;
case '&': flag = true; break;
}
}
}
return(flag);
}
}
8.2 Example: HTML-Filtering Tag
 csajsp-taglib-adv.tld
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns=http://java.sun.com/xml/ns/j2ee xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-
jsptaglibrary_2_0.xsd"
version="2.0">
<tlib-version>1.0</tlib-version>
<short-name>csajsp-taglib-adv</short-name>
<uri>http://coreservlets.com/csajsp-taglib-adv</uri>
<tag>
<description>Converts special HTML characters to their corresponding HTML character
entities.</description>
<name>filterhtml</name>
<tag-class>coreservlets.tags.HtmlFilterTag</tag-class>
<body-content>scriptless</body-content>
</tag>
</taglib>
8.2 Example: HTML-Filtering Tag
 html-filter.jsp
<TABLE BORDER=1 ALIGN="CENTER">
<TR CLASS="COLORED"><TH>Example<TH>Result
<%@ taglib uri="/WEB-INF/tlds/csajsp-taglib-adv.tld“ prefix="csajsp" %>
<TR>
<TD><PRE><csajsp:filterhtml>
<STRONG>Some strongly emphasized text.</STRONG><BR>
<CODE>Some code.</CODE><BR>
<SAMP>Some sample text.</SAMP><BR>
<KBD>Some keyboard text.</KBD><BR>
</csajsp:filterhtml></PRE>
<TD>
<STRONG>Some strongly emphasized text.</STRONG><BR>
<CODE>Some code.</CODE><BR>
<SAMP>Some sample text.</SAMP><BR>
<KBD>Some keyboard text.</KBD><BR>
</TABLE>
8.2 Example: HTML-Filtering Tag
8.3 Assigning Dynamic Values to Tag Attributes
 In this section, we show you how to change that so the
we can pass dynamic values to our tag.
 In other words, we would like to be able to call our
custom tag with a construct like the following:
 <prefix:name attribute1="${bean.value}"
attribute2="<%= bean.getValue() %>" />
8.3 Assigning Dynamic Values to Tag Attributes
 Dynamic Attribute Values: Tag Handler Class
 As far as the tag handler class is concerned, there is no
difference.
 You still need to provide a setter method for the attribute in
the form setXxx(String value), where Xxx is the name of
the attribute with the first character capitalized.
public void setAttribute1(String value1)
{
doSomethingWith(value1);
}
8.3 Assigning Dynamic Values to Tag Attributes
 Dynamic Attribute Values: Tag Library Descriptor
 As before, each attribute needs to be declared inside the TLD.
 However, because we want to allow the JSP author to specify dynamic (or
runtime) expressions as values for the attributes, we must specify rtexprvalue to
be true, like in the following:
<tag>
<description>...</description>
<name>mytag</name>
<tag-class>package.TagHandler</tag-class>
<body-content>...</body-content>
<attribute>
<description>...</description>
<name>attribute1</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
8.3 Assigning Dynamic Values to Tag Attributes
 Dynamic Attribute Values: JSP File
 As before, the JSP page has to declare the tag library using
the taglib directive.
 Note, however, that this does not mean that only JSP
expressions are allowed to be placed as values for those
attributes. Good old static string values, placed there
during the authoring of the page, are still allowed.
 Attributes that accept runtime content are allowed to be
JSP scripting expressions and JSP EL.
<prefix:name attribute1="${bean.value}"
attribute2="<%= bean.getValue() %>" />
8.4 Example: Simple Looping Tag
 In this example, we create a simple for loop tag that
outputs its tag body the number of times that the count
attribute specifies.
 The count attribute is declared to accept runtime
expressions.
 We create the following files,
 CoinBean.java ( Bean Class )
 SimpleLoopTest.java ( Servlet )
 ForTag.java ( Tag Handler Class )
 csajsp-taglib-adv.tld ( TLD file )
 simple-loop-test.jsp ( JSP file)
8.4 Example: Simple Looping Tag
 CoinBean.java ( Bean Class )
package coreservlets;
import java.io.*;
public class CoinBean implements Serializable
{
public String getFlip()
{
if (Math.random() < 0.5)
{return("Heads");}
else
{return("Tails"); }
}
}
8.4 Example: Simple Looping Tag
 SimpleLoopTest.java ( Servlet )
package coreservlets;
import java.io.*; import javax.servlet.*; import javax.servlet.http.*;
public class SimpleLoopTest extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException
{
CoinBean coin = new CoinBean();
request.setAttribute("coin", coin);
RequestDispatcher dispatcher =
request.getRequestDispatcher("/WEB-INF/JSTL/simple-loop-test.jsp");
dispatcher.forward(request, response);
}
}
8.4 Example: Simple Looping Tag
 ForTag.java ( Tag Handler Class )
package coreservlets.tags;
import javax.servlet.jsp.*;import javax.servlet.jsp.tagext.*; import java.io.*;
public class ForTag extends SimpleTagSupport
{
private int count;
public void setCount(int count)
{ this.count = count; }
public void doTag() throws JspException, IOException
{
for(int i=0; i<count; i++)
{
getJspBody().invoke(null);
}
}
}
8.4 Example: Simple Looping Tag
 csajsp-taglib-adv.tld ( TLD file )
<tag>
<description>Loops specified number of times.</description>
<name>for</name>
<tag-class>coreservlets.tags.ForTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<description>Number of times to repeat body.</description>
<name>count</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
8.4 Example: Simple Looping Tag
 simple-loop-test.jsp ( JSP file )
<BODY>
<H1>Simple Loop Test</H1>
<%@ taglib uri="/WEB-INF/tlds/csajsp-taglib-adv.tld“ prefix="csajsp" %>
<H2>A Very Important List</H2>
<UL>
<csajsp:for count="<%=(int)(Math.random()*10)%>">
<LI>Blah
</csajsp:for>
</UL>
<H2>Some Coin Flips</H2>
<UL>
<csajsp:for count="<%=(int)(Math.random()*10)%>">
<LI>${coin.flip}
</csajsp:for>
</UL>
</BODY>
8.4 Example: Simple Looping Tag
8.5 Assigning Complex Objects asValues to Tag
Attributes
 If we want to pass a Collection of Orders or some other
complex object structure than what would we have to change to
accommodate this, that is discussed in this section.
 Complex Dynamic AttributeValues: Tag Handler Class
 The type of the argument in the setter would now be the complex object
type instead of String,like the following:
public void setAttribute1(SomeComplexObject value1)
{
doSomethingWith(value1);
}
8.5 Assigning Complex Objects asValues to Tag
Attributes
 Complex Dynamic Attribute Values:TLD
 The TLD stays the same ,but make sure to provide the
rtexprvalue element with a value of true.
<attribute>
<description>...</description>
<name>attribute1</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
8.5 Assigning Complex Objects asValues to Tag
Attributes
 Complex Dynamic Attribute Values: JSP File
 As before, the JSP page has to declare the tag library using
the taglib directive.
 <%@ taglib uri="..." prefix="..." %>
 The usage of the tag is very much the same as when we had
dynamic values that were strings.
 <prefix:name attribute1="${bean.value}“
attribute2="<%= bean.getValue() %>" />
8.6 Example: Table Formatting Tag
 Complex Dynamic Attribute Values: JSP File
 In this example, we list the three most recent swimming
world records.
 To list the records, we employ the use of a custom table-
formatting tag that lets the JSP author pass the entire record
set to the tag as a two-dimensional array object.
 We create the following files,
 WorldRecords.java ( To retrive records)
 ShowRecords.java ( Servlet )
 MakeTableTag.java ( Tag Handler Class )
 csajsp-taglib-adv.tld ( TLD file )
 show-records.jsp (JSP file)
8.6 Example: Table Formatting Tag
8.7 Creating Looping Tags
 Consider the following typical Java looping structure. In it, we
use a for loop to iterate through an array of strings.
for (int i; i < someArray.length; i++)
{
System.out.print("Object at position " + i + "is: ");
System.out.print(someArray[i]);
}
 The for loop construct exposes the looping index i to its body.
However, no code outside of the for loop body is able to access
i because its scope is limited tothe body of the loop, delimited
by the curly braces.
8.7 Creating Looping Tags
 This construct is very useful inside a JSP page as well. In this
case, the looping structure would be the custom tag.
 This tag would create some bean with appropriate values and
pass it to the JSP content inside the body of the tag.
 This is done inside the doTag method with the use of the tag
body scope attribute.
 The tag body scope is similar to the request or application
scope in nature, except that its attributes are only visible to the
tag body.
 Any JSP code outside of the custom tag’s beginning and ending
elements would not be able to access it.
 You use the following code inside the doTag method to place
an object as an attribute of the tag body scope:
 getJspContext().setAttribute(key, object);
8.7 Creating Looping Tags
 You then use JSP EL inside the body of the
custom tag to access this attribute.
 This is no different than accessing any other
scoped bean.
 You just have to remember that this attribute is
not available outside the confines of the tag
body.
<prefix:custom-tag>
some text ${someBean.someValue}
</prefix:custom-tag>
8.7 Creating Looping Tags
 When creating a looping tag, it is also very common to provide
the author of the JSP page with an attribute they can set, which
lets them pass the name of the attribute they will later access in
the tag body; that is, let them specify the string value of the key
argument that gets passed into
getJspContext().setAttribute(key, object).
 For example:
<mytags:for beanKeyValue="arrayValue" iterateOver="${someArray}">
Value is: ${arrayValue}
</mytags:for>
8.8 Example: ForEach Tag
 In this example, we create a forEach custom tag that
can iterate over arrays of any objects.
 foreach tag has two attributes,
 Item :- The array of elements.
 var :- The name of the scoped variable to which each entry
is assigned.
 We create the following files,
 LoopTest.java (servlet)
 ForEachTag.java (Tag Handler Class)
 csajsp-taglib-foreach.tld (TLD file)
 loop-test.jsp (JSP file)
8.8 Example: ForEach Tag
 LoopTest.java (servlet)
package coreservlets;
public class LoopTest extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException
{
String[] servers ={"Tomcat", "Resin", "JRun", "WebLogic",
"WebSphere", "Oracle 10g", "JBoss" };
request.setAttribute("servers", servers);
Object[][] records = WorldRecords.getRecentRecords();
request.setAttribute("records", records);
RequestDispatcher dispatcher =
request.getRequestDispatcher("/WEB-INF/results/loop-test.jsp");
dispatcher.forward(request, response);
}
}
8.8 Example: ForEach Tag
 ForEachTag.java (Tag Handler Class)
package coreservlets.tags;
public class ForEachTag extends SimpleTagSupport
{
private Object[] items;
private String attributeName;
public void setItems(Object[] items)
{ this.items = items; }
public void setVar(String attributeName)
{ this.attributeName = attributeName; }
public void doTag() throws JspException, IOException
{
for(int i=0; i<items.length; i++)
{
getJspContext().setAttribute(attributeName, items[i]);
getJspBody().invoke(null);
}
}
}
8.8 Example: ForEach Tag
 csajsp-taglib-foreach.tld (TLD file)
<tag>
<description>Loops each element in array</description>
<name>forEach</name>
<tag-class>coreservlets.tags.ForEachTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>var</name>
<required>true</required>
</attribute>
</tag>
8.8 Example: ForEach Tag
 loop-test.jsp (JSP file)
<BODY> <H1>Loop Test</H1>
<%@ taglib uri="/WEB-INF/tlds/csajsp-taglib-adv.tld“ prefix="csajsp" %>
<H2>Some Java-Based Servers</H2>
<UL>
<csajsp:forEach items="${servers}" var="server">
<LI>${server}
</csajsp:forEach>
</UL>
<H2>Recent World Records</H2>
<TABLE BORDER=1>
<csajsp:forEach items="${records}" var="row">
<TR>
<csajsp:forEach items="${row}" var="col">
<TD>${col}</TD>
</csajsp:forEach>
</TR>
</csajsp:forEach>
</TABLE>
</BODY>
8.8 Example: ForEach Tag
8.9 Creating Expression Language Functions
 In the Example: Debug Tag, we have created a debug
tag.
 This simple tag surrounds some debugging
information inside the JSP page.
 If the debug request parameter is present, the
contents of the debug tag are allowed to be processed
and output to the JSP page.
8.9 Creating Expression Language Functions
 There is, however, one limitation to our debug tag. As with
all custom tags based on the SimpleTag API, the body of the
tag is not allowed to contain any JSP scripting.
 The only way we can output something is through JSP
EL,which requires that the object has bean-like getter methods.
 We can create such a bean in the doTag method of the debug tag
handler class, but this would require us to update the tag handler class
code every time we need to output some new debugging information or
modify the current output.
 Furthermore, we would like to reuse the same debug tag on multiple
JSP pages. Placing the information we want to see for one page into the
tag handler class would require us to create multiple debug tags for
different JSP pages.
8.9 Creating Expression Language Functions
 There are two sensible solutions to this problem,
 Usage of nested custom tags
 Use an EL function
 ELfunctions are a new feature to the JSP specification 2.0.
 EL functions allow the developer to call a static method inside
the JSP page, but instead of using JSP scripting to do it, which
is illegal inside the tag body, EL functions allow the developer
touse EL notation to invoke the function.
 Inside the JSP page, an EL function invocationwould look like
the following:
 ${prefix:someFunction(package.ClassName argument)}
8.9 Creating Expression Language Functions
 The steps for creating an EL function
1. Create a class with a public static method.
2. Declare the method inside a TLD file.
3. Declare tag library inside the JSP page.
4. Use JSP EL to invoke the method.
8.9 Creating Expression Language Functions
1. Create a class with a public static method
 This class should be placed into the WEB-INF/classes
directory.
public class SomeClass
{
public static void someMethod() {...}
}
8.9 Creating Expression Language Functions
2. Declare the method inside a TLD file.
 The fully qualified class name and one of its methods gets mapped to
some name that will be used inside the JSP page to invoke that method.
 However, instead of the tag element, which declares a tag, EL
functions use the function element and its related elements as follows:
<function>
<name>run</name>
<function-class>somePackage.SomeClass</function-class>
<function-signature>void someMethod()</function-signature>
</function>
 The value of the function-signature element should contain the
signature of the method with all the specified types using their fully
qualified class notation.
<function-signature>
void someMethod(java.lang.String)
</function-signature>
8.9 Creating Expression Language Functions
3. Declare tag library inside the JSP page.
 This step is identical to declaring a TLD that contains
declarations of custom tags exclusively.
 Just like before you use the taglib directive, assign a prefix
to be used throughout the JSP page. For example:
 <%@ taglib prefix="myfunc" uri="someURI" %>
8.9 Creating Expression Language Functions
4. Use JSP EL to invoke the method.
 The invocation of the method is done in the following form:
 ${myfunc:run()}
 The myfunc part is the prefix that comes from the taglib
declaration mentioned in Step 3.
 The run part comes from the name element inside the
function element declaration in the TLD.
 Note that the function name used inside the JSP page does
not have to be the same as the method name inside the
implementing class.
8.10 Handling Nested Custom Tags
 So far we have seen custom tags whose bodies
contained ordinary JSP content.
 However, the tag body of a custom tag can also
contain other custom tags, as follows:
<prefix:tagOuter>
<prefix:tagInner>
...
</prefix:tagInner>
</prefix:tagOuter>
8.10 Handling Nested Custom Tags
 The SimpleTag API provides two methods that let an
inner tag get a hold of the outer tag.
 The first method is getParent().
 It is called on the instance of the inner tag.
 It returns an object of type JspTag, which can be cast to the type of
the outer tag’s handler class. If this method is called on the most
outer tag in the hierarchy, it returns null.
 Although you can keep calling the getParent method over and over
until you either reach the tag instance you are seeking or null.
8.10 Handling Nested Custom Tags
 The second method is
 findAncestorWithClass(JspTag fromTag, Class
toBeMatchedClass)
 This method starts searching the hierarchy of tags from a given tag
instance, fromTag, looking at its parent tag up until it finds a tag
instance that matches the type of toBeMatchedClass.
 If the search takes it all the way to the top of the hierarchy with no
results, it returns null.
 If this method finds the right instance of the tag class, it returns it
as a JspTag instance, which needs to be cast to the proper type.
 Note :- With the getParent method, you need to
anticipate a ClassCastException because you don’t
know the parent tag’s type in advance.

Contenu connexe

Tendances

Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to JavascriptAnjan Banda
 
比XML更好用的Java Annotation
比XML更好用的Java Annotation比XML更好用的Java Annotation
比XML更好用的Java Annotationjavatwo2011
 
A Re-Introduction to JavaScript
A Re-Introduction to JavaScriptA Re-Introduction to JavaScript
A Re-Introduction to JavaScriptSimon Willison
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming LanguageRaghavan Mohan
 
Spring 3: What's New
Spring 3: What's NewSpring 3: What's New
Spring 3: What's NewTed Pennings
 
Lambda: A Peek Under The Hood - Brian Goetz
Lambda: A Peek Under The Hood - Brian GoetzLambda: A Peek Under The Hood - Brian Goetz
Lambda: A Peek Under The Hood - Brian GoetzJAX London
 
Java EE 與 雲端運算的展望
Java EE 與 雲端運算的展望 Java EE 與 雲端運算的展望
Java EE 與 雲端運算的展望 javatwo2011
 
Javascript best practices
Javascript best practicesJavascript best practices
Javascript best practicesManav Gupta
 
Bt0083 server side programing 2
Bt0083 server side programing  2Bt0083 server side programing  2
Bt0083 server side programing 2Techglyphs
 
eXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction TrainingeXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction TrainingHoat Le
 
Java Web Programming [8/9] : JSF and AJAX
Java Web Programming [8/9] : JSF and AJAXJava Web Programming [8/9] : JSF and AJAX
Java Web Programming [8/9] : JSF and AJAXIMC Institute
 

Tendances (20)

Clean coding-practices
Clean coding-practicesClean coding-practices
Clean coding-practices
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to Javascript
 
Javascript
JavascriptJavascript
Javascript
 
Java Script Best Practices
Java Script Best PracticesJava Script Best Practices
Java Script Best Practices
 
比XML更好用的Java Annotation
比XML更好用的Java Annotation比XML更好用的Java Annotation
比XML更好用的Java Annotation
 
A Re-Introduction to JavaScript
A Re-Introduction to JavaScriptA Re-Introduction to JavaScript
A Re-Introduction to JavaScript
 
Javascript
JavascriptJavascript
Javascript
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming Language
 
Spring 3: What's New
Spring 3: What's NewSpring 3: What's New
Spring 3: What's New
 
Lambda: A Peek Under The Hood - Brian Goetz
Lambda: A Peek Under The Hood - Brian GoetzLambda: A Peek Under The Hood - Brian Goetz
Lambda: A Peek Under The Hood - Brian Goetz
 
Java EE 與 雲端運算的展望
Java EE 與 雲端運算的展望 Java EE 與 雲端運算的展望
Java EE 與 雲端運算的展望
 
Javascript best practices
Javascript best practicesJavascript best practices
Javascript best practices
 
Java Persistence API
Java Persistence APIJava Persistence API
Java Persistence API
 
JPA 2.0
JPA 2.0JPA 2.0
JPA 2.0
 
Bt0083 server side programing 2
Bt0083 server side programing  2Bt0083 server side programing  2
Bt0083 server side programing 2
 
Os Leonard
Os LeonardOs Leonard
Os Leonard
 
IOC + Javascript
IOC + JavascriptIOC + Javascript
IOC + Javascript
 
eXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction TrainingeXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction Training
 
Java Web Programming [8/9] : JSF and AJAX
Java Web Programming [8/9] : JSF and AJAXJava Web Programming [8/9] : JSF and AJAX
Java Web Programming [8/9] : JSF and AJAX
 
Advance Java
Advance JavaAdvance Java
Advance Java
 

En vedette

Laurent présentation stage
Laurent présentation stageLaurent présentation stage
Laurent présentation stageLaurent Samuel
 
Intellicore Tech Talk 10 - Apache Web Server Internals
Intellicore Tech Talk 10 - Apache Web Server InternalsIntellicore Tech Talk 10 - Apache Web Server Internals
Intellicore Tech Talk 10 - Apache Web Server InternalsNeil Armstrong
 
Servlet and jsp interview questions
Servlet and jsp interview questionsServlet and jsp interview questions
Servlet and jsp interview questionsSujata Regoti
 
Aerohive - La mort annoncée du contrôleur
Aerohive - La mort annoncée du contrôleurAerohive - La mort annoncée du contrôleur
Aerohive - La mort annoncée du contrôleurppuichaud
 
Présentation de Apache Zookeeper
Présentation de Apache ZookeeperPrésentation de Apache Zookeeper
Présentation de Apache ZookeeperMichaël Morello
 
Architecture d’une app qui fait 5 millions de visites par mois
Architecture d’une app qui fait 5 millions de visites par moisArchitecture d’une app qui fait 5 millions de visites par mois
Architecture d’une app qui fait 5 millions de visites par moisJulien Carnelos
 
The java server pages
The java server pagesThe java server pages
The java server pagesAtul Saurabh
 
JBoss Application Server 7
JBoss Application Server 7JBoss Application Server 7
JBoss Application Server 7Ray Ploski
 
Apache Kafka, Un système distribué de messagerie hautement performant
Apache Kafka, Un système distribué de messagerie hautement performantApache Kafka, Un système distribué de messagerie hautement performant
Apache Kafka, Un système distribué de messagerie hautement performantALTIC Altic
 

En vedette (16)

Jsp Presentation +Mufix "3"
Jsp Presentation +Mufix "3"Jsp Presentation +Mufix "3"
Jsp Presentation +Mufix "3"
 
JSP Technology I
JSP Technology IJSP Technology I
JSP Technology I
 
Laurent présentation stage
Laurent présentation stageLaurent présentation stage
Laurent présentation stage
 
Intellicore Tech Talk 10 - Apache Web Server Internals
Intellicore Tech Talk 10 - Apache Web Server InternalsIntellicore Tech Talk 10 - Apache Web Server Internals
Intellicore Tech Talk 10 - Apache Web Server Internals
 
Final Table of Content
Final Table of ContentFinal Table of Content
Final Table of Content
 
Servlet and jsp interview questions
Servlet and jsp interview questionsServlet and jsp interview questions
Servlet and jsp interview questions
 
Aerohive - La mort annoncée du contrôleur
Aerohive - La mort annoncée du contrôleurAerohive - La mort annoncée du contrôleur
Aerohive - La mort annoncée du contrôleur
 
Présentation de Apache Zookeeper
Présentation de Apache ZookeeperPrésentation de Apache Zookeeper
Présentation de Apache Zookeeper
 
Chap4 4 2
Chap4 4 2Chap4 4 2
Chap4 4 2
 
Architecture d’une app qui fait 5 millions de visites par mois
Architecture d’une app qui fait 5 millions de visites par moisArchitecture d’une app qui fait 5 millions de visites par mois
Architecture d’une app qui fait 5 millions de visites par mois
 
The java server pages
The java server pagesThe java server pages
The java server pages
 
Squid
SquidSquid
Squid
 
APACHE HTTP
APACHE HTTPAPACHE HTTP
APACHE HTTP
 
APACHE TOMCAT
APACHE TOMCATAPACHE TOMCAT
APACHE TOMCAT
 
JBoss Application Server 7
JBoss Application Server 7JBoss Application Server 7
JBoss Application Server 7
 
Apache Kafka, Un système distribué de messagerie hautement performant
Apache Kafka, Un système distribué de messagerie hautement performantApache Kafka, Un système distribué de messagerie hautement performant
Apache Kafka, Un système distribué de messagerie hautement performant
 

Similaire à Jstl 8

Implementing jsp tag extensions
Implementing jsp tag extensionsImplementing jsp tag extensions
Implementing jsp tag extensionsSoujanya V
 
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 8 ...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 8 ...Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 8 ...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 8 ...WebStackAcademy
 
Jsp standard tag_library
Jsp standard tag_libraryJsp standard tag_library
Jsp standard tag_libraryKP Singh
 
Basics of Java Script (JS)
Basics of Java Script (JS)Basics of Java Script (JS)
Basics of Java Script (JS)Ajay Khatri
 
Implementing java server pages standard tag library v2
Implementing java server pages standard tag library v2Implementing java server pages standard tag library v2
Implementing java server pages standard tag library v2Soujanya V
 
DataBase Connectivity
DataBase ConnectivityDataBase Connectivity
DataBase ConnectivityAkankshaji
 
JSP Standard Tag Library
JSP Standard Tag LibraryJSP Standard Tag Library
JSP Standard Tag LibraryVISHAL DONGA
 
Introduction to JSP
Introduction to JSPIntroduction to JSP
Introduction to JSPGeethu Mohan
 
Java 7 new features
Java 7 new featuresJava 7 new features
Java 7 new featuresShivam Goel
 
Introducing Struts 2
Introducing Struts 2Introducing Struts 2
Introducing Struts 2wiradikusuma
 

Similaire à Jstl 8 (20)

Jsp session 9
Jsp   session 9Jsp   session 9
Jsp session 9
 
Implementing jsp tag extensions
Implementing jsp tag extensionsImplementing jsp tag extensions
Implementing jsp tag extensions
 
Jsp and jstl
Jsp and jstlJsp and jstl
Jsp and jstl
 
JSTL.pptx
JSTL.pptxJSTL.pptx
JSTL.pptx
 
Session_15_JSTL.pdf
Session_15_JSTL.pdfSession_15_JSTL.pdf
Session_15_JSTL.pdf
 
Jsp element
Jsp elementJsp element
Jsp element
 
Java 17
Java 17Java 17
Java 17
 
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 8 ...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 8 ...Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 8 ...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 8 ...
 
Jsp standard tag_library
Jsp standard tag_libraryJsp standard tag_library
Jsp standard tag_library
 
Basics of Java Script (JS)
Basics of Java Script (JS)Basics of Java Script (JS)
Basics of Java Script (JS)
 
Jsp in Servlet by Rj
Jsp in Servlet by RjJsp in Servlet by Rj
Jsp in Servlet by Rj
 
Implementing java server pages standard tag library v2
Implementing java server pages standard tag library v2Implementing java server pages standard tag library v2
Implementing java server pages standard tag library v2
 
Introduction to JSP.pptx
Introduction to JSP.pptxIntroduction to JSP.pptx
Introduction to JSP.pptx
 
DataBase Connectivity
DataBase ConnectivityDataBase Connectivity
DataBase Connectivity
 
JSP Standard Tag Library
JSP Standard Tag LibraryJSP Standard Tag Library
JSP Standard Tag Library
 
Introduction to JSP
Introduction to JSPIntroduction to JSP
Introduction to JSP
 
Java Script ppt
Java Script pptJava Script ppt
Java Script ppt
 
Java 7 new features
Java 7 new featuresJava 7 new features
Java 7 new features
 
Introducing Struts 2
Introducing Struts 2Introducing Struts 2
Introducing Struts 2
 
RicoLiveGrid
RicoLiveGridRicoLiveGrid
RicoLiveGrid
 

Dernier

Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 

Dernier (20)

Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 

Jstl 8

  • 2. Topics in This Chapter  Manipulating tag body  Assigning dynamic values to tag attributes  Assigning complex objects as values to tag attributes  Creating looping tags  Creating expression language functions  Working with nested custom tags
  • 3. 8.1 Manipulating Tag Body  In Chapter 7 we have seen how to include the body of the tag in the output of the Java-based custom tag.  To review, there are essentially two things you need to do.  Specify scriptless in the body-content element of the TLD for the tag.  This allows the page author to insert JSP content between the opening and closing elements of the tag.  Remember that this JSPcontent is not allowed to have any scripting elements like <% ... %> or <%= ... %>.  Invoke the output of the tag body by calling getJspBody().invoke(null) inside the doTag method of the tag handler class.  Remember that this statement passes the output of the JSP content to the client, not the actual JSP code itself.
  • 4. 8.1 Manipulating Tag Body  The invoke method takes a Writer as its argument.  If null is passed to the invoke method, the container directs the output of the tag body to the JspWriter object.  The server obtains a reference to this object by calling methods similar to getJspContext().getOut(). In other words, the statements getJspBody().invoke(null); and getJspBody().invoke(getJspContext().getOut()); accomplish exactly the same results.
  • 5. 8.1 Manipulating Tag Body  Pass a different Writer to the invoke method.  Using the new Writer, we can buffer up the output of the JSP content, extract it from the Writer, manipulate it, and output the new content to the client.  The following are the steps of how this can be accomplished.  Create an instance of a convenient Writer. Any class that inherits from java.io.Writer class is acceptable. Because the output to the client is usually HTML, which is just a string, the java.io.String- Writer class is the most common Writer to use.  For example: StringWriter myWriter = new StringWriter();  Pass the newly created StringWriter as an argument to the invoke method.  For example: getJspBody().invoke(myWriter);  Note that now the output of the tag body is not sent to the client but buffered up inside myWriter.  Extract the buffered output from the Writer, modify it, and send the  modified version to the client like so:  String modified = modify(myWriter.toString());  getJspContext().getOut().print(modified);
  • 6. 8.2 Example: HTML-Filtering Tag  In this example, we create a custom tag, filterhtml, that filters out the HTML code, converting it to regular text, thus preventing the browser from interpreting it as HTML code. We are now able to see the actual unrendered HTML code .  We define 3 components as follows,  Tag Handler Class (HtmlFilterTag)  TLD file (csajsp-taglib-adv.tld)  JSP page (html-filter.jsp)
  • 7. 8.2 Example: HTML-Filtering Tag  Tag Handler Class : HtmlFilterTag.java package coreservlets.tags; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.io.*; import coreservlets.ServletUtilities; public class HtmlFilterTag extends SimpleTagSupport { public void doTag() throws JspException, IOException { StringWriter stringWriter = new StringWriter(); getJspBody().invoke(stringWriter); String output =ServletUtilities.filter(stringWriter.toString()); JspWriter out = getJspContext().getOut(); out.print(output); } }
  • 8. 8.2 Example: HTML-Filtering Tag  ServletUtilities.java package coreservlets; import javax.servlet.*;import javax.servlet.http.*; public class ServletUtilities { public static String filter(String input) { if (!hasSpecialChars(input)) { return(input); } StringBuffer filtered = new StringBuffer(input.length()); char c; for(int i=0; i<input.length(); i++) { c = input.charAt(i); switch(c) { case '<': filtered.append("&lt;"); break; case '>': filtered.append("&gt;"); break; case '"': filtered.append("&quot;"); break; case '&': filtered.append("&amp;"); break; default: filtered.append(c); } } return(filtered.toString()); }
  • 9. 8.2 Example: HTML-Filtering Tag private static boolean hasSpecialChars(String input) { boolean flag = false; if ((input != null) && (input.length() > 0)) { char c; for(int i=0; i<input.length(); i++) { c = input.charAt(i); switch(c) { case '<': flag = true; break; case '>': flag = true; break; case '"': flag = true; break; case '&': flag = true; break; } } } return(flag); } }
  • 10. 8.2 Example: HTML-Filtering Tag  csajsp-taglib-adv.tld <?xml version="1.0" encoding="UTF-8" ?> <taglib xmlns=http://java.sun.com/xml/ns/j2ee xmlns:xsi="http://www.w3.org/2001/XMLSchema- instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web- jsptaglibrary_2_0.xsd" version="2.0"> <tlib-version>1.0</tlib-version> <short-name>csajsp-taglib-adv</short-name> <uri>http://coreservlets.com/csajsp-taglib-adv</uri> <tag> <description>Converts special HTML characters to their corresponding HTML character entities.</description> <name>filterhtml</name> <tag-class>coreservlets.tags.HtmlFilterTag</tag-class> <body-content>scriptless</body-content> </tag> </taglib>
  • 11. 8.2 Example: HTML-Filtering Tag  html-filter.jsp <TABLE BORDER=1 ALIGN="CENTER"> <TR CLASS="COLORED"><TH>Example<TH>Result <%@ taglib uri="/WEB-INF/tlds/csajsp-taglib-adv.tld“ prefix="csajsp" %> <TR> <TD><PRE><csajsp:filterhtml> <STRONG>Some strongly emphasized text.</STRONG><BR> <CODE>Some code.</CODE><BR> <SAMP>Some sample text.</SAMP><BR> <KBD>Some keyboard text.</KBD><BR> </csajsp:filterhtml></PRE> <TD> <STRONG>Some strongly emphasized text.</STRONG><BR> <CODE>Some code.</CODE><BR> <SAMP>Some sample text.</SAMP><BR> <KBD>Some keyboard text.</KBD><BR> </TABLE>
  • 13. 8.3 Assigning Dynamic Values to Tag Attributes  In this section, we show you how to change that so the we can pass dynamic values to our tag.  In other words, we would like to be able to call our custom tag with a construct like the following:  <prefix:name attribute1="${bean.value}" attribute2="<%= bean.getValue() %>" />
  • 14. 8.3 Assigning Dynamic Values to Tag Attributes  Dynamic Attribute Values: Tag Handler Class  As far as the tag handler class is concerned, there is no difference.  You still need to provide a setter method for the attribute in the form setXxx(String value), where Xxx is the name of the attribute with the first character capitalized. public void setAttribute1(String value1) { doSomethingWith(value1); }
  • 15. 8.3 Assigning Dynamic Values to Tag Attributes  Dynamic Attribute Values: Tag Library Descriptor  As before, each attribute needs to be declared inside the TLD.  However, because we want to allow the JSP author to specify dynamic (or runtime) expressions as values for the attributes, we must specify rtexprvalue to be true, like in the following: <tag> <description>...</description> <name>mytag</name> <tag-class>package.TagHandler</tag-class> <body-content>...</body-content> <attribute> <description>...</description> <name>attribute1</name> <rtexprvalue>true</rtexprvalue> </attribute> </tag>
  • 16. 8.3 Assigning Dynamic Values to Tag Attributes  Dynamic Attribute Values: JSP File  As before, the JSP page has to declare the tag library using the taglib directive.  Note, however, that this does not mean that only JSP expressions are allowed to be placed as values for those attributes. Good old static string values, placed there during the authoring of the page, are still allowed.  Attributes that accept runtime content are allowed to be JSP scripting expressions and JSP EL. <prefix:name attribute1="${bean.value}" attribute2="<%= bean.getValue() %>" />
  • 17. 8.4 Example: Simple Looping Tag  In this example, we create a simple for loop tag that outputs its tag body the number of times that the count attribute specifies.  The count attribute is declared to accept runtime expressions.  We create the following files,  CoinBean.java ( Bean Class )  SimpleLoopTest.java ( Servlet )  ForTag.java ( Tag Handler Class )  csajsp-taglib-adv.tld ( TLD file )  simple-loop-test.jsp ( JSP file)
  • 18. 8.4 Example: Simple Looping Tag  CoinBean.java ( Bean Class ) package coreservlets; import java.io.*; public class CoinBean implements Serializable { public String getFlip() { if (Math.random() < 0.5) {return("Heads");} else {return("Tails"); } } }
  • 19. 8.4 Example: Simple Looping Tag  SimpleLoopTest.java ( Servlet ) package coreservlets; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class SimpleLoopTest extends HttpServlet { public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { CoinBean coin = new CoinBean(); request.setAttribute("coin", coin); RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/JSTL/simple-loop-test.jsp"); dispatcher.forward(request, response); } }
  • 20. 8.4 Example: Simple Looping Tag  ForTag.java ( Tag Handler Class ) package coreservlets.tags; import javax.servlet.jsp.*;import javax.servlet.jsp.tagext.*; import java.io.*; public class ForTag extends SimpleTagSupport { private int count; public void setCount(int count) { this.count = count; } public void doTag() throws JspException, IOException { for(int i=0; i<count; i++) { getJspBody().invoke(null); } } }
  • 21. 8.4 Example: Simple Looping Tag  csajsp-taglib-adv.tld ( TLD file ) <tag> <description>Loops specified number of times.</description> <name>for</name> <tag-class>coreservlets.tags.ForTag</tag-class> <body-content>scriptless</body-content> <attribute> <description>Number of times to repeat body.</description> <name>count</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag>
  • 22. 8.4 Example: Simple Looping Tag  simple-loop-test.jsp ( JSP file ) <BODY> <H1>Simple Loop Test</H1> <%@ taglib uri="/WEB-INF/tlds/csajsp-taglib-adv.tld“ prefix="csajsp" %> <H2>A Very Important List</H2> <UL> <csajsp:for count="<%=(int)(Math.random()*10)%>"> <LI>Blah </csajsp:for> </UL> <H2>Some Coin Flips</H2> <UL> <csajsp:for count="<%=(int)(Math.random()*10)%>"> <LI>${coin.flip} </csajsp:for> </UL> </BODY>
  • 23. 8.4 Example: Simple Looping Tag
  • 24. 8.5 Assigning Complex Objects asValues to Tag Attributes  If we want to pass a Collection of Orders or some other complex object structure than what would we have to change to accommodate this, that is discussed in this section.  Complex Dynamic AttributeValues: Tag Handler Class  The type of the argument in the setter would now be the complex object type instead of String,like the following: public void setAttribute1(SomeComplexObject value1) { doSomethingWith(value1); }
  • 25. 8.5 Assigning Complex Objects asValues to Tag Attributes  Complex Dynamic Attribute Values:TLD  The TLD stays the same ,but make sure to provide the rtexprvalue element with a value of true. <attribute> <description>...</description> <name>attribute1</name> <rtexprvalue>true</rtexprvalue> </attribute>
  • 26. 8.5 Assigning Complex Objects asValues to Tag Attributes  Complex Dynamic Attribute Values: JSP File  As before, the JSP page has to declare the tag library using the taglib directive.  <%@ taglib uri="..." prefix="..." %>  The usage of the tag is very much the same as when we had dynamic values that were strings.  <prefix:name attribute1="${bean.value}“ attribute2="<%= bean.getValue() %>" />
  • 27. 8.6 Example: Table Formatting Tag  Complex Dynamic Attribute Values: JSP File  In this example, we list the three most recent swimming world records.  To list the records, we employ the use of a custom table- formatting tag that lets the JSP author pass the entire record set to the tag as a two-dimensional array object.  We create the following files,  WorldRecords.java ( To retrive records)  ShowRecords.java ( Servlet )  MakeTableTag.java ( Tag Handler Class )  csajsp-taglib-adv.tld ( TLD file )  show-records.jsp (JSP file)
  • 28. 8.6 Example: Table Formatting Tag
  • 29. 8.7 Creating Looping Tags  Consider the following typical Java looping structure. In it, we use a for loop to iterate through an array of strings. for (int i; i < someArray.length; i++) { System.out.print("Object at position " + i + "is: "); System.out.print(someArray[i]); }  The for loop construct exposes the looping index i to its body. However, no code outside of the for loop body is able to access i because its scope is limited tothe body of the loop, delimited by the curly braces.
  • 30. 8.7 Creating Looping Tags  This construct is very useful inside a JSP page as well. In this case, the looping structure would be the custom tag.  This tag would create some bean with appropriate values and pass it to the JSP content inside the body of the tag.  This is done inside the doTag method with the use of the tag body scope attribute.  The tag body scope is similar to the request or application scope in nature, except that its attributes are only visible to the tag body.  Any JSP code outside of the custom tag’s beginning and ending elements would not be able to access it.  You use the following code inside the doTag method to place an object as an attribute of the tag body scope:  getJspContext().setAttribute(key, object);
  • 31. 8.7 Creating Looping Tags  You then use JSP EL inside the body of the custom tag to access this attribute.  This is no different than accessing any other scoped bean.  You just have to remember that this attribute is not available outside the confines of the tag body. <prefix:custom-tag> some text ${someBean.someValue} </prefix:custom-tag>
  • 32. 8.7 Creating Looping Tags  When creating a looping tag, it is also very common to provide the author of the JSP page with an attribute they can set, which lets them pass the name of the attribute they will later access in the tag body; that is, let them specify the string value of the key argument that gets passed into getJspContext().setAttribute(key, object).  For example: <mytags:for beanKeyValue="arrayValue" iterateOver="${someArray}"> Value is: ${arrayValue} </mytags:for>
  • 33. 8.8 Example: ForEach Tag  In this example, we create a forEach custom tag that can iterate over arrays of any objects.  foreach tag has two attributes,  Item :- The array of elements.  var :- The name of the scoped variable to which each entry is assigned.  We create the following files,  LoopTest.java (servlet)  ForEachTag.java (Tag Handler Class)  csajsp-taglib-foreach.tld (TLD file)  loop-test.jsp (JSP file)
  • 34. 8.8 Example: ForEach Tag  LoopTest.java (servlet) package coreservlets; public class LoopTest extends HttpServlet { public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { String[] servers ={"Tomcat", "Resin", "JRun", "WebLogic", "WebSphere", "Oracle 10g", "JBoss" }; request.setAttribute("servers", servers); Object[][] records = WorldRecords.getRecentRecords(); request.setAttribute("records", records); RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/results/loop-test.jsp"); dispatcher.forward(request, response); } }
  • 35. 8.8 Example: ForEach Tag  ForEachTag.java (Tag Handler Class) package coreservlets.tags; public class ForEachTag extends SimpleTagSupport { private Object[] items; private String attributeName; public void setItems(Object[] items) { this.items = items; } public void setVar(String attributeName) { this.attributeName = attributeName; } public void doTag() throws JspException, IOException { for(int i=0; i<items.length; i++) { getJspContext().setAttribute(attributeName, items[i]); getJspBody().invoke(null); } } }
  • 36. 8.8 Example: ForEach Tag  csajsp-taglib-foreach.tld (TLD file) <tag> <description>Loops each element in array</description> <name>forEach</name> <tag-class>coreservlets.tags.ForEachTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>items</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>var</name> <required>true</required> </attribute> </tag>
  • 37. 8.8 Example: ForEach Tag  loop-test.jsp (JSP file) <BODY> <H1>Loop Test</H1> <%@ taglib uri="/WEB-INF/tlds/csajsp-taglib-adv.tld“ prefix="csajsp" %> <H2>Some Java-Based Servers</H2> <UL> <csajsp:forEach items="${servers}" var="server"> <LI>${server} </csajsp:forEach> </UL> <H2>Recent World Records</H2> <TABLE BORDER=1> <csajsp:forEach items="${records}" var="row"> <TR> <csajsp:forEach items="${row}" var="col"> <TD>${col}</TD> </csajsp:forEach> </TR> </csajsp:forEach> </TABLE> </BODY>
  • 39. 8.9 Creating Expression Language Functions  In the Example: Debug Tag, we have created a debug tag.  This simple tag surrounds some debugging information inside the JSP page.  If the debug request parameter is present, the contents of the debug tag are allowed to be processed and output to the JSP page.
  • 40. 8.9 Creating Expression Language Functions  There is, however, one limitation to our debug tag. As with all custom tags based on the SimpleTag API, the body of the tag is not allowed to contain any JSP scripting.  The only way we can output something is through JSP EL,which requires that the object has bean-like getter methods.  We can create such a bean in the doTag method of the debug tag handler class, but this would require us to update the tag handler class code every time we need to output some new debugging information or modify the current output.  Furthermore, we would like to reuse the same debug tag on multiple JSP pages. Placing the information we want to see for one page into the tag handler class would require us to create multiple debug tags for different JSP pages.
  • 41. 8.9 Creating Expression Language Functions  There are two sensible solutions to this problem,  Usage of nested custom tags  Use an EL function  ELfunctions are a new feature to the JSP specification 2.0.  EL functions allow the developer to call a static method inside the JSP page, but instead of using JSP scripting to do it, which is illegal inside the tag body, EL functions allow the developer touse EL notation to invoke the function.  Inside the JSP page, an EL function invocationwould look like the following:  ${prefix:someFunction(package.ClassName argument)}
  • 42. 8.9 Creating Expression Language Functions  The steps for creating an EL function 1. Create a class with a public static method. 2. Declare the method inside a TLD file. 3. Declare tag library inside the JSP page. 4. Use JSP EL to invoke the method.
  • 43. 8.9 Creating Expression Language Functions 1. Create a class with a public static method  This class should be placed into the WEB-INF/classes directory. public class SomeClass { public static void someMethod() {...} }
  • 44. 8.9 Creating Expression Language Functions 2. Declare the method inside a TLD file.  The fully qualified class name and one of its methods gets mapped to some name that will be used inside the JSP page to invoke that method.  However, instead of the tag element, which declares a tag, EL functions use the function element and its related elements as follows: <function> <name>run</name> <function-class>somePackage.SomeClass</function-class> <function-signature>void someMethod()</function-signature> </function>  The value of the function-signature element should contain the signature of the method with all the specified types using their fully qualified class notation. <function-signature> void someMethod(java.lang.String) </function-signature>
  • 45. 8.9 Creating Expression Language Functions 3. Declare tag library inside the JSP page.  This step is identical to declaring a TLD that contains declarations of custom tags exclusively.  Just like before you use the taglib directive, assign a prefix to be used throughout the JSP page. For example:  <%@ taglib prefix="myfunc" uri="someURI" %>
  • 46. 8.9 Creating Expression Language Functions 4. Use JSP EL to invoke the method.  The invocation of the method is done in the following form:  ${myfunc:run()}  The myfunc part is the prefix that comes from the taglib declaration mentioned in Step 3.  The run part comes from the name element inside the function element declaration in the TLD.  Note that the function name used inside the JSP page does not have to be the same as the method name inside the implementing class.
  • 47. 8.10 Handling Nested Custom Tags  So far we have seen custom tags whose bodies contained ordinary JSP content.  However, the tag body of a custom tag can also contain other custom tags, as follows: <prefix:tagOuter> <prefix:tagInner> ... </prefix:tagInner> </prefix:tagOuter>
  • 48. 8.10 Handling Nested Custom Tags  The SimpleTag API provides two methods that let an inner tag get a hold of the outer tag.  The first method is getParent().  It is called on the instance of the inner tag.  It returns an object of type JspTag, which can be cast to the type of the outer tag’s handler class. If this method is called on the most outer tag in the hierarchy, it returns null.  Although you can keep calling the getParent method over and over until you either reach the tag instance you are seeking or null.
  • 49. 8.10 Handling Nested Custom Tags  The second method is  findAncestorWithClass(JspTag fromTag, Class toBeMatchedClass)  This method starts searching the hierarchy of tags from a given tag instance, fromTag, looking at its parent tag up until it finds a tag instance that matches the type of toBeMatchedClass.  If the search takes it all the way to the top of the hierarchy with no results, it returns null.  If this method finds the right instance of the tag class, it returns it as a JspTag instance, which needs to be cast to the proper type.  Note :- With the getParent method, you need to anticipate a ClassCastException because you don’t know the parent tag’s type in advance.