In this tutorial you will learn how to create custom tag.
Custom Tag Example
In this tutorial you will learn how to create custom tag.
Here I am going to give an example which will demonstrate you how to create a custom tag in JSP. To create a custom tag at first we should have a tag handler so I have created a tag handler class named SimpleTagHandler.java which will implements a custom tag, and then created a TLD (Tag Library Descriptor) file named simpleTag.tld in WEB-INF directory using which the web container / web server will validate the tag, then created a JSP page.
SimpleTagHandler.java
package pack;
import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.jsp.JspWriter;
public class SimpleTagHandler extends TagSupport
{
public int doStartTag()
{
JspWriter jw = pageContext.getOut();
try
{
jw.println("<br>Body of doStartTag()");
}
catch(Exception e)
{
System.out.println(e);
}
return EVAL_BODY_INCLUDE;
}
public int doEndTag()
{
JspWriter jw = pageContext.getOut();
try
{
jw.println("<br>Body of doEndTag()");
}
catch(Exception e)
{
System.out.println(e);
}
return EVAL_PAGE;
}
}
customTagExample.jsp
<%@taglib uri="/WEB-INF/simpleTag.tld" prefix="dev" %> <html> <head> <title>simple custom tag example</title> </head> <body> <dev:mytag> <br><b>Body of JSP page</b> </dev:mytag> </body> </html>
simpleTag.tld
<taglib> <tlib-version>1.0</tlib-version> <jsp-version>2.0</jsp-version> <tag> <name>mytag</name> <tag-class>pack.SimpleTagHandler</tag-class> </tag> </taglib>
Output :
When you will execute the jsp file customTagExample.jsp you will get the output as :


[ 0 ] Comments