In this tutorial you will learn about TagSupport class
Custom Tag TagSupport
In this tutorial you will learn about TagSupport class
TagSupport is a class of javax.servlet.jsp.tagext and implements the Tag & IterationTag interface. Since this class implements the Tag & IterationTag interface so all the methods for supporting a tag handler class is implemented already besides these methods this class provides to add an additional functionality for Tag properties. This utility class is used as a base class to define a new tag handlers. When you will use the TagSupport following static final integer constant value are returned by the doStartTag() & doEndTag() method :
1. Returned value by the doStartTag() method :
- SKIP_BODY
- EVAL_BODY_INCLUDE
1. Returned value by the doEndTag() method :
- EVAL_PAGE
- SKIP_PAGE
Example :
Here I am giving a simple example which will demonstrate you how to use the TagSupport class. To achieve this problem at first I have created a TagHandler class named TagSupportExample.java into which I defined the methods to handle the tag. Then after created a TLD file named tagSupportExample.tld in WEB-INF directory using which the web container / web server will validate the tag, then created a JSP page named tagSupportExample.jsp.
TagSupportExample.java
package pack; import javax.servlet.jsp.tagext.TagSupport; public class TagSupportExample extends TagSupport { public int doStartTag() { return EVAL_BODY_INCLUDE; } public int doEndTag() { return SKIP_PAGE; } }
tagSupportExample.tld
<taglib> <tlib-version>1.0</tlib-version> <jsp-version>2.0</jsp-version> <tag> <name>simple</name> <tag-class>pack.TagSupportExample</tag-class> </tag> </taglib>
tagSupportExample.jsp
<%@taglib uri="/WEB-INF/tagSupportExample.tld" prefix="dev" %> <html> <head> <title>TagSupport Example</title> </head> <body> <b>Jsp page content before starting tag.</b> <dev:simple> <br><b>Body of custom tag </b> </dev:simple> <br><b>Jsp page content after closing tag.</b> </body> </html>
Output :
When you will execute the above jsp file you will get the output as :
[ 0 ] Comments