This document discusses two approaches to creating custom tag libraries in JavaServer Pages (JSP): Java-based and JSP-based tags. Java-based tags use Java files for tag handling and have no version restrictions, while JSP-based tags use JSP files and are only available from JSP 2.0 onward. Both approaches involve a tag handler class, tag library descriptor (TLD) file, and JSP file using the tag library. The tag handler class defines the tag behavior by implementing the doTag() method. The TLD maps XML element names to the tag implementation. Attributes can be passed to tags and accessed via setter methods. Tag files provide an alternative to Java classes for simple formatting tags.
2. Two Approach
Java Based Custom Tag
No Version Restriction
Uses Java file for Tag Handling
Preferred when lot of java code is
required for getting output
JSP Based Custom Tag
Only from JSP 2.0
Uses JSP file for Tag Handling
Preferred when lot of html code is
required for getting output.
Prof. AshishSingh Bhatia 2
3. Tag Library Components
Tag handler class that defines the tag behavior.
The TLD file that maps the XML element names to the tag implementation.
The JSP file that uses the tag library.
3Prof. AshishSingh Bhatia
4. The Tag Handler Class
Class that tells what to do when system see the tag.
Class must implement SimpleTag interface.
In practice, extends SimpleTagSupport which implements SimpleTag.
javax.servlet.jsp.tagext package.
Every Tag Handler class must have 0 argument constructor.
doTag() is the main method for tag handling.
We need JspWriter [ getJspContext().getOut() ]
New instance is created for every tag occurrence on the page.
Prof. AshishSingh Bhatia 4
9. Assigning Attributes to Tags
For every attribute we need a set method in tag handler class
<prefix:tag attribute1=value1 attribute2=value2 />
Prof. AshishSingh Bhatia 9
setAttribute1(String value) setAttribute2(String value)
11. Example : Custom tag to reverse a String
public class StringReverseTag extends SimpleTagSupport {
private String data;
public void setData(String data) {
this.data=data;
}
public void doTag() throws JspException, IOException {
JspWriter out = getJspContext().getOut();
StringBuffer sb = new StringBuffer(data);
sb.reverse();
out.print(sb);
}
}
Prof. AshishSingh Bhatia 11
14. Including Tag body in Tag Output
getJspBody().invoke(null)
null means the resulting output of that JSP content is passed verbatim to the client.
doTag() has no way to access the tag body output.
Example
In JSP File : <ashish:tag> This is the test </ashish:tag>
In Java File
out.print(<b>Hello World</b> <br/>);
getJSPBody().invoke(null);
out.print(<b>This is my tag</b>);
In Tag File : <body-content>scriptless</body-content>
Prof. AshishSingh Bhatia 14
15. Using Tag Files
Java Based Custom Tag
What we have seen is Java Based Custom Tag
Tag handler class is Java File
JSP Based Custom Tag [ Tag Files ]
Tag Handler class is JSP file
When to use which ?
Simple Rule : Use Java Based Custom Tag when lot of java code is involved. IF more
is of formatting use JSP Based Custom Tag.
Remember Tag files run only on JSP 2.0.
Java Base Tag have no such restriction.
Prof. AshishSingh Bhatia 15
16. JSP Based Custom Tag
Create a JSP Base tag file
Create a JSP page that uses the tag file
Prof. AshishSingh Bhatia 16
18. helloworld.tag and index.jsp
Tag File [ helloworld.tag ]
<b>Hello World</b>
JSP File
<html>
<head><title>Tag Example</title></head>
<body>
<%@ taglib tagdir="/WEB-INF/tags" prefix="ashish" %>
<ashish:helloworld/>
</body>
</html>
Prof. AshishSingh Bhatia 18
19. String Reverse [ Using Attribute and Tag File ]
Tag File [ reverse.tag ]
<%@ attribute name="data" required="true" %>
<%
StringBuffer sb = new StringBuffer(data);
sb.reverse();
%>
<%= sb %>
JSP File
<%@ taglib tagdir="/WEB-INF/tags" prefix="ashish" %>
<ashish:reverse data="EARTH"/>
Prof. AshishSingh Bhatia 19
20. Using Body part of the tag using Tag File
Use <jsp:Body/> to get the out put of body
Example
In JSP File : <ashish:test> This is the test </ashish:test>
In Tag File [ test.tag ] :
<b>Hello World</b> <br/>
<jsp:doBody/>
<b>This is my tag</b>
Prof. AshishSingh Bhatia 20