Académique Documents
Professionnel Documents
Culture Documents
Outline
Defined
Motivation
Tag Types
Tag Handlers
Tag Library Descriptors
Using Tags in JSPs
Examples
What are Custom Tags?
Better Packaging
By improving the separation between
business logic and presentation.
Encapsulate common functionality
Over a large number of pages and reuse
it. This applies to both HTML
presentation and application logic.
Motivation
P r e s e n ta tio n Im p le m e n ta tio n
HTM L and
H T M L - lik e Ja v a B e a n s
JS P ta g s
C u s to m
JS P T a g s
What can they do?
int doStartTag()
Called by JSP runtime for Tag handler to
process the start tag for this instance.
int doEndTag()
Called by JSP runtime after returning from
doStartTag(). The body of the action may
or may not have been evaluated,
depending on the return value of
doStartTag().
Interface Tag Methods
Tag getParent()
Get the parent (closest enclosing tag
handler) for this tag handler.
void release()
Called on a Tag handler to perform any
cleanup operation.
Interface Tag Methods
import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class HelloTag extends TagSupport {
public int doStartTag() throws
JspException{
try {
// Java code
} catch (IOException ioe) {
Template for Tag Without Body
import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class HelloTag extends TagSupport {
public int doStartTag() throws
JspException{
try {
// Java code
} catch (IOException ioe) {
Template for Tag With Body
O b t a in
h a n d le r
E V A L _ B O D Y _ IN C L U D E
d o S ta r tT a g ( )
S et s e tP a g e C o n te x t( )
p r o p e r t ie s s e tP a r e n t( ) S K IP _ B O D Y P ro c ess b o d y
S e t a t t r ib u t e
v a lu e s
S K IP _ P A G E EVAL_PAG E
d o E n d T ag ()
r e le a s e ( ) r e le a s e ( )
S to p C o n t in u e
Example
package test;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;
public class MyFontTag extends TagSupport
{
private String size ="2"; // default size is 2
private String color="#0000FF"; // default color is blue
public void setSize(String str) { this.size = str; }
public void setColor(String str) { this.color = str; }
MyFontTag.java
<?xml version="1.0"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.// DTD
JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>ctag</shortname>
<tag>
<name>myfont</name>
<tagclass>test.MyFontTag</tagclass>
<bodycontent>JSP</bodycontent>
Mytaglib.tld
<attribute>
<name>color</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>size</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>
Referencing Tags
<HTML>
<HEAD><TITLE>Tag Lib Example</TITLE></HEAD>
<BODY>
<%@ taglib uri="mytaglib.tld" prefix="ctag" %>
To make use of custom tag, in JSP write… <BR>
<ctag:myfont>With default attributes</ctag:myfont>
<BR>Or if you want to change the values from default
<BR>
<ctag:myfont color="#FF0000" size="4">
With given attributes
</ctag:myfont>
</BODY>
</HTML>
Page Compilation
Page
C o m p ile r
t a g l i b d ir e c t iv e ? new TLD? lo a d T L D
c u s to m ta g ? v a lid a t e a t t r ib u t e s y n t a x
T a g E x t r a In f o ? id e n t if y s c r ip t in g v a r ia b le s
a s s o c ia t e v a r ia b le n a m e s a n d c la s s e s
Page Execution
R equest
t a g l i b d ir e c t iv e ? n o a c t io n r e q u ir e d
c u s to m ta g ? n e w ta g ? lo a d t a g h a n d le r
a d d in s t a n c e ( s ) t o r e s o u r c e p o o l
R esponse o b t a in h a n d le r f r o m p o o l
in it ia liz e h a n d le r f o r t a g
e x e c u t e h a n d le r
r e t u r n h a n d le r t o p o o l
Running JSP
Tag
Library
JAR
directories
containing
tag handler
packages
MANIFEST.MF
META-INF
taglib.tld
Tags With Body
Tag
implements
Iteration Tag TagSupport
implements
Body Tag BodyTagSupport
BodyTag Life Cycle
O b t a in
h a n d le r
I n it ia liz e s e tB o d y C o n te n t( )
b o d y c o n te n t d o I n it B o d y ( )
S et s e tP a g e C o n te x t( ) d o S ta r tT a g ( )
EVAL_BO D Y_T AG
p r o p e r t ie s s e tP a r e n t()
S K IP _ BO D Y P ro c ess b o d y
S e t a t t r ib u t e
v a lu e s
d o A f te r B o d y ( )
S K IP _ BO D Y EVAL_BO D Y_T AG
S K IP _ P A G E EVAL_PAG E
d o E n d T ag ()
r e le a s e ( ) r e le a s e ( )
S to p C o n t in u e
Single Evaluation
package test;
import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
TLD
<tag>
<name>tolowercase</name>
<tagclass>test.ToLowerCaseTag</tagclass>
<bodycontent>JSP</bodycontent>
<info>To lower case tag</info>
</tag>
Single Evaluation
<HTML>
<HEAD><TITLE>To Lower Case Example </TITLE>
</HEAD>
<BODY>
JSP Custom Tag Programming<BR>
<%@ taglib uri="mytaglib.tld" prefix="ctag" %>
<ctag:tolowercase>
JSP Custom Tag Programming
</ctag:tolowercase>
</BODY>
</HTML>
Single Evaluation
Result
JSP Custom Tag Programming
jsp custom tag programming
Single Evaluation
Result
JSP Custom Tag Programming
jsp custom tag programming
Single Evaluation
<HTML>
<HEAD><TITLE>To Lower Case Example </TITLE>
</HEAD>
<BODY>
JSP Custom Tag Programming<BR>
<%@ taglib uri="mytaglib.tld" prefix="ctag" %>
<ctag:tolowercase>
<ctag:myfont color="#FF0000" size="4">
JSP Custom Tag Programming
</ctag:myfont>
</ctag:tolowercase>
</BODY>
</HTML>
Single Evaluation
Result
...
taglib uri="mytags" prefix="codecamp"
%>
<OL>
<!-- Repeats N times. A null reps value
means repeat once. -->
<codecamp:repeat reps='<%=
request.getParameter("repeats") %>'>
<LI><codecamp:prime length="40" />
</codecamp:repeat>
</OL>...
Multiple Evaluation
package test;
import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class IterTag extends BodyTagSupport {
int times = 0;
BodyContent bodyContent;
public void setTimes(int times) { this.times = times; }
public int doStartTag() throws JspException
{
return times >1?EVAL_BODY_AGAIN:SKIP_BODY;
}
Multiple Evaluation
<tag>
<name>iter</name>
<tagclass>test.IterTag</tagclass>
<bodycontent>JSP</bodycontent>
<info>Tag with body and parameter</info>
<attribute>
<name>times</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
Multiple Evaluation
Result
package test;
import java.util.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public final class SecondTag implements Tag {
private PageContext pc = null;
public void setPageContext(PageContext p) {
pc = p;
}
public void setParent(Tag t) {}
public Tag getParent() { return null; }
Scripting variables
package test;
import javax.servlet.jsp.tagext.*;
public class SecondTagTEI extends TagExtraInfo {
public VariableInfo[] getVariableInfo(TagData data) {
return new VariableInfo[] {
new VariableInfo("time", "java.lang.String",
true, VariableInfo.NESTED)
};
}
}
Scripting variables
<tag>
<name>secondtag</name>
<tagclass>test.SecondTag</tagclass>
<teiclass>test.SecondTagTEI</teiclass>
<bodycontent>JSP</bodycontent>
<info>Your second JSP Tag</info>
</tag>
Scripting variables
<html><head<title>SecondTag</title></head>
<body>
<%@ taglib uri="mytaglib.tld" prefix="star"%>
<star:secondtag>
<p>Date value retrieved from JSP Tag:<%=
time %></p>
</star:secondtag>
</body>
</html>
Examples
<%@ taglib
uri="http://jakarta.apache.org/taglibs/dbta
gs" prefix="sql" %>
<%-- open a db connection --%>
<sql:connection id="conn1">
<sql:url>jdbc:mysql://localhost/test</sql:u
rl>
<sql:driver>org.gjt.mm.mysql.Driver</sql:
driver>
</sql:connection>
Examples