In this tutorial you will learn how to use doEndTag() in jsp to handle custom tags.
Custom tag doEndTag() Example
In this tutorial you will learn how to use doEndTag() in jsp to handle custom tags.
Tag handler method doEndTag() is defined by a Tag interface that is called by the Jsp page's servlet when an end tag is evaluated. doEndTag() method is invoked when an end tag of custom tag is found. This function returns static final integer constant value that are already defined in the interface such as :
- SKIP_PAGE : It is an optional returned value but this value must be returned by doEndTag() when you do not want to execute rest of the jsp page contents after the closing custom tag.
- EVAL_PAGE : It is an optional returned value but this value must be returned by the doEndTag() when you want to execute rest of the jsp page contents after the closing custom tag.
Example :
Here I am giving a simple example which will demonstrate you how to use the doEndTag() tag handler method and the constant integer values. To do so at first I have created a tag handler class named DoEndTagExample.java that extends the TagSupport class ( I have used here the TagSupport class to define a tag handler class because it is easy to use, you can use the Tag interface but if you will use this interface you will be required to implement all the methods defined in it ) here in this example I used only the doEndTag() method so in the tag handler class it will encountered when on the jsp page a closed custom tag will be evaluated or instantiated then I have created a TLD file named doEndTagExample.tld in WEB-INF directory using which the web container / web server will validate the tag, then created a JSP page named doEndTagExample.jsp.
DoEndTagExample.java
package pack; import javax.servlet.jsp.tagext.TagSupport; public class DoEndTagExample extends TagSupport { public int doEndTag() { return EVAL_PAGE; //return SKIP_PAGE; } }
doEndTagExample.tld
<taglib> <tlib-version>1.0</tlib-version> <jsp-version>2.0</jsp-version> <tag> <name>doEnd</name> <tag-class>pack.DoEndTagExample</tag-class> </tag> </taglib>
doEndTagExample.jsp
<%@taglib uri="/WEB-INF/doEndTagExample.tld" prefix="dev" %> <html> <head> <title>doEndTag() Example</title> </head> <body> Jsp page content before the custom tag. <dev:doEnd /> <br>Jsp page content after closing tag. </body> </html>
Output :
When you will execute the above jsp file you will get the output as :
1. When you will use the constant value EVAL_PAGE then the output will be as :
2. When you will use the constant value SKIP_PAGE then the output will be as :
[ 0 ] Comments