Tags & Tag Libraries
•JSP technology has a set of pre- defined tags
–<jsp: useBean …/>
•These are HTML like but…
•… have limited functionality
•Can define new tags
–Look like HTML
–Can be used by page authors
–“Java code” is executed when tag is encountered
–Allow us to keep Java code off the page
•Better separation of content and logic
May Have Tags To…
•Process an SQL command
•Parse XML and output HTML
•Automatically call into an “EJB component” (EJB ™ technology- based component)
•Get called on every request to initialize script variables
•Iterate over a ResultSet and display the output in an HTML table
To define a tag, you need to:
Tag Handlers:
A tag handler is an object invoked by a Web container to evaluate a custom tag during the execution of the JSP page that references the tag. Tag handlers must implement either the Tag or BodyTag interface.
For newly created handlers, you can use the TagSupport and BodyTagSupport classes as base classes. These classes and interfaces are contained in the javax.servlet.jsp.tagext package.
Tag handler methods defined by the Tag and BodyTag interfaces are called by the JSP page's servlet at various points during the evaluation of the tag. When the start tag of a custom tag is encountered, the JSP page's servlet calls methods to initialize the appropriate handler and then invokes the handler's doStartTag method. When the end tag of a custom tag is encountered, the handler's doEndTag method is invoked. Additional methods are invoked in between when a tag handler needs to interact with the body of the tag.
Simple Tag Example :
<%@ taglib uri=“/WEB-INF/mylib.tld” prefix=“test” %>
<html>
<body bgcolor=“white”>
<test:hello name=“Java Pandit” />
</body>
</html>
public class HelloTag extends TagSupport {
private String name = “World”;
public void setName(String name) { this.name = name; }
public int doEndTag() { pageContext.getOut().println(“Hello “ + name); }
}
mylib.tld
<taglib> ……
<tag><name>hello</name>
<tagclass>com.pramati.HelloTag</tagclass>
<bodycontent>empty</bodycontent>
<attribute><name>name</name></attribute>
</tag>
</taglib>
How Tag Handler methods are invoked :
<prefix:tagName
attr1=“value1” ------------ setAttr1(“value1”)
attr2=“value2” ------------ setAttr2(“value2”)
> ------------ doStartTag()
This tags's body
</ prefix:tagName>------------ doEndTag()
•Implementation of JSP page will use the tag handler for each ‘action’ on page.
Summary
•The JSP specification is a powerful system for creating structured web content
•JSP technology allows non- programmers to develop dynamic web pages
•JSP technology allows collaboration between programmers and page designers when building web applications
•JSP technology uses the Java programming language as the script language
•The generated servlet can be managed by directives
•JSP components can be used as the view in the MVC architecture
•Authors using JSP technology are not necessarily programmers using Java technology
•Want to keep “Java code” off a “JSP Page”
•Custom actions (tag libraries) allow the use of elements as a replacement for Java code
•JSP technology has a set of pre- defined tags
–<jsp: useBean …/>
•These are HTML like but…
•… have limited functionality
•Can define new tags
–Look like HTML
–Can be used by page authors
–“Java code” is executed when tag is encountered
–Allow us to keep Java code off the page
•Better separation of content and logic
May Have Tags To…
•Process an SQL command
•Parse XML and output HTML
•Automatically call into an “EJB component” (EJB ™ technology- based component)
•Get called on every request to initialize script variables
•Iterate over a ResultSet and display the output in an HTML table
To define a tag, you need to:
- Develop a tag handler and helper classes for the tag
- Declare the tag in a tag library descriptor
Tag Handlers:
A tag handler is an object invoked by a Web container to evaluate a custom tag during the execution of the JSP page that references the tag. Tag handlers must implement either the Tag or BodyTag interface.
For newly created handlers, you can use the TagSupport and BodyTagSupport classes as base classes. These classes and interfaces are contained in the javax.servlet.jsp.tagext package.
Tag handler methods defined by the Tag and BodyTag interfaces are called by the JSP page's servlet at various points during the evaluation of the tag. When the start tag of a custom tag is encountered, the JSP page's servlet calls methods to initialize the appropriate handler and then invokes the handler's doStartTag method. When the end tag of a custom tag is encountered, the handler's doEndTag method is invoked. Additional methods are invoked in between when a tag handler needs to interact with the body of the tag.
Simple Tag Example :
<%@ taglib uri=“/WEB-INF/mylib.tld” prefix=“test” %>
<html>
<body bgcolor=“white”>
<test:hello name=“Java Pandit” />
</body>
</html>
public class HelloTag extends TagSupport {
private String name = “World”;
public void setName(String name) { this.name = name; }
public int doEndTag() { pageContext.getOut().println(“Hello “ + name); }
}
mylib.tld
<taglib> ……
<tag><name>hello</name>
<tagclass>com.pramati.HelloTag</tagclass>
<bodycontent>empty</bodycontent>
<attribute><name>name</name></attribute>
</tag>
</taglib>
How Tag Handler methods are invoked :
<prefix:tagName
attr1=“value1” ------------ setAttr1(“value1”)
attr2=“value2” ------------ setAttr2(“value2”)
> ------------ doStartTag()
This tags's body
</ prefix:tagName>------------ doEndTag()
•Implementation of JSP page will use the tag handler for each ‘action’ on page.
Summary
•The JSP specification is a powerful system for creating structured web content
•JSP technology allows non- programmers to develop dynamic web pages
•JSP technology allows collaboration between programmers and page designers when building web applications
•JSP technology uses the Java programming language as the script language
•The generated servlet can be managed by directives
•JSP components can be used as the view in the MVC architecture
•Authors using JSP technology are not necessarily programmers using Java technology
•Want to keep “Java code” off a “JSP Page”
•Custom actions (tag libraries) allow the use of elements as a replacement for Java code