Download - JSP : Creating Custom Tag
CUSTOM TAG LIBRARY
Prof. AshishSingh Bhatia
1Prof. AshishSingh Bhatia
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
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
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
Directory Structure
hellowordtag
index.jsp
WEB-INF
tlds tag.tld
classes mytag HelloWorldTag.java
Prof. AshishSingh Bhatia 5
Tag Handler Class
package mytag;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
public class HelloWorldTag extends SimpleTagSupport {
public void doTag() throws JspException, IOException {
JspWriter out = getJspContext().getOut();
out.print("<b>Hello World</b>");
}
}
Prof. AshishSingh Bhatia 6
TLD : Tag Library Descriptor
<?xml version="1.0" ?>
<taglib version="2.0">
<tlib-version>1.0</tlib-version>
<short-name>mytag</short-name>
<tag>
<description>HelloWorld Tag</description>
<name>helloworld</name>
<tag-class>mytag.HelloWorldTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
Prof. AshishSingh Bhatia 7
Optional
Required Element
empty, scriptless, tagdependent, JSP
JSP File
<html>
<head>
<title>Tag Example</title>
</head>
<body>
<%@ taglib uri="/WEB-INF/tlds/tag.tld" prefix="ashish" %>
<ashish:helloworld/>
</body>
</html>
Prof. AshishSingh Bhatia 8
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)
TLD File
<attribute>
<description>…. </description>
<name> X </name>
<required> true/false </required>
</attribute>
Prof. AshishSingh Bhatia 10
X = Must match with the variable in class
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
TLD File
<tag>
<description>StringReverse Tag</description>
<name>string</name>
<tag-class>mytag.StringReverseTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>data</name>
<required>true</required>
</attribute>
</tag>
Prof. AshishSingh Bhatia 12
JSP FILE
<html>
<head>
<title>Tag Example</title>
</head>
<body>
<%@ taglib uri="/WEB-INF/tlds/tag.tld" prefix="ashish" %>
<ashish:string data="EARTH"/>
</body>
</html>
Prof. AshishSingh Bhatia 13
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
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
JSP Based Custom Tag
Create a JSP Base tag file
Create a JSP page that uses the tag file
Prof. AshishSingh Bhatia 16
Structure
tagdemo
index.jsp
WEB-INF tags helloworld.tag
Prof. AshishSingh Bhatia 17
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
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
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
END OF SESSION
21Prof. AshishSingh Bhatia