JSP – Create Custom Tags
JSP Tags acts as a plugin to your JSP pages. These are basically Java Classes that get executed when jsp page get rendered by server and browser. JSP comes with in build Tags like jsp:include, jsp:forward, but they are not sufficient to cater to the Web World. To overcome this problem you can design tags based on your business requirement. This article will guide you how we can create an custom tags for JSP.
JSP Custom Tags:
All JSP Custom Tags implements Tag Interface. To know more information on Tag Interface refer to JSP Custom Tag Interface. But we don’t directly use Tag Interface instead we use TagSupport and BodyTagSupport, this is because some default implementation of Tag Interface is already implemented in TagSupport and BodyTagSupport so it becomes easier to write custom tag code.
Difference between TagSupport and BodyTagSupport:
In TagSupport we define all the attributes required to display the data where as in BodyTagSupport we pass attributes and body content to the JSP Tag. BodyTagSupport is not used frequently unless you want to manipulate the body content. Passing attributes to the custom tags is not mandatory.
Example of TagSupport:
<test :showTime />Example of BodyTagSupport:
<test :showTime> Current Time: </test>
If you see in BodyTagSupport we pass some body content to the xml tag. Though body content is not mandatory but it will degrade the performance if you not using body for your custom tag and you are not using TagSupport.
Important Function Call in JSP Custom Tag Code:
doStartTag():
This function get called when the jsp tag get initialized. This function an integer value which is already defined in Tag Interface in form of variable name. We normally return SKIP_BODY. SKIP_BODY tells the jsp engine to skip the body of the jsp tag.
doEndTag():
This function get called just after doStartTag() function processing is done. Similar to doStartTag() this function also returns an integer value. Normally we return EVAL_BODY. EVAL_BODY tells the jsp engine to process the remaining jsp code.
Simple Custom Tag Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | package com.hiteshagrawal; import java.io.*; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.TagSupport; public class Test extends TagSupport { public int doStartTag() throws JspException { try { JspWriter out = pageContext.getOut(); out.print("This is my first tag!"); } catch (IOException ioe) { ioe.printStackTrace(); throw new JspException("IOException while writing data to page" + ioe.getMessage()); } return SKIP_BODY; } public int doEndTag() throws JspException { return EVAL_PAGE; } } |
Code Explanation:
- Class extends TagSupport indicating we are not using BodyTagSupport class.
- doStartTag() Function writes an String content using pageContext.getOut(), this forms the output that will get written on your jsp page.
- doStartTag() Function returns SKIP_BODY. The value for this variable is already defined in Tag Interface.
- doEndTag() Function returns EVAL_PAGE. The value for this variable is already defined in Tag Interface.
Implementing JSP Tag Java Code in JSP:
Now our next step would be to create an Tag Descriptor Library (TLD) file. This file tells the JSP Engine about the JSP Custom Tag that will get used inside the JSP Code.
< ?xml version="1.0" encoding="ISO-8859-1" ?> < !DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"> <taglib> <tlib -version>1.0</tlib> <jsp -version>1.2</jsp> <short -name>test</short> <tag> <name>TestingTag</name> <tagclass>com.hiteshagrawal.Test</tagclass> <bodycontent>empty</bodycontent> </tag> </taglib>
If you see i have defined an TLD file for the above JSP code written. Your Custom JSP Tag must be written inside “tag” element. Let me explaing the written xml file:
- tlib-version – As the name suggest it refers to the Tag Library Version
- jsp-version – Refers to the JSP version you are using
- short-name – Refers to the name of the JSP Custom Tag that your jsp code will be referring to
- name – Refers to the name the Custom Tags
- tagclass – Refersn to the class structure used for your JSP Tags
- bodycontent – This is used in case we are using BodyTagSupport, in TagSupport we keep this empty
Save this TLD file by name test.tld inside your WEB-INF folder.
Reference TLD File in JSP Page:
There are multiple ways to refererence TLD file in JSP and i am discussing one of them in this article.
JSP Implementing Custom JSP Tags:
< %@ taglib prefix="test" uri="/WEB-INF/test.tld"%> <html> <head> <title>JSP Custom Tag Demo</title> </head> <body> <test :TestingTag /> </body> </html>
JSP Code Explanation:
If you see i am making reference to the taglib which will get called by name “test”. Also i am passing the path of the TLD file it will be referring to.
Finally i am calling the Custom Tag as shown in line no: 7
JSP Output:
When we executed the above JSP code we get the following output.
This is my first tag!
















Actually the easiest way to implement them is using tag files. IWebMvc makes heavy use of tags, both created from files and/or classes. Take a look if interested in some “real world” examples.
See also Coldtags suite: http://www.servletsuite.com/jsp.htm
It is a largest set of custom JSP tags over the Net.
Good tutorial.
Nice tutorial, easy to understand. Thanks for the same.