how to use JSP Custom Tag

Example of JSP Custom Tag

In this example, we are going to create a custom tag that prints the current date and time. We are performing action at the start of tag.
For creating any custom tag, we need to follow following steps:
  1. Create the Tag handler class and perform action at the start or at the end of the tag.
  2. Create the Tag Library Descriptor (TLD) file and define tags
  3. Create the JSP file that uses the Custom tag defined in the TLD file

Understanding flow of custom tag in jsp

flow of custom tag

1) Create the Tag handler class

To create the Tag Handler, we are inheriting the TagSupport class and overriding its method doStartTag().To write data for the jsp, we need to use the JspWriter class.
The PageContext class provides getOut() method that returns the instance of JspWriter class. TagSupport class provides instance of pageContext bydefault.
File: MyTagHandler.java
  1. package com.javatpoint.sonoo;  
  2. import java.util.Calendar;  
  3. import javax.servlet.jsp.JspException;  
  4. import javax.servlet.jsp.JspWriter;  
  5. import javax.servlet.jsp.tagext.TagSupport;  
  6. public class MyTagHandler extends TagSupport{  
  7.   
  8. public int doStartTag() throws JspException {  
  9.     JspWriter out=pageContext.getOut();//returns the instance of JspWriter  
  10.     try{  
  11.      out.print(Calendar.getInstance().getTime());//printing date and time using JspWriter  
  12.     }catch(Exception e){System.out.println(e);}  
  13.     return SKIP_BODY;//will not evaluate the body content of the tag  
  14. }  
  15. }  

2) Create the TLD file

Tag Library Descriptor (TLD) file contains information of tag and Tag Hander classes. It must be contained inside the WEB-INF directory.
File: mytags.tld
  1. <?xml version="1.0" encoding="ISO-8859-1" ?>  
  2. <!DOCTYPE taglib  
  3.         PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"  
  4.     "http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd">  
  5.   
  6. <taglib>  
  7.   
  8.   <tlib-version>1.0</tlib-version>  
  9.   <jsp-version>1.2</jsp-version>  
  10.   <short-name>simple</short-name>  
  11.   <uri>http://tomcat.apache.org/example-taglib</uri>  
  12.   
  13. <tag>  
  14. <name>today</name>  
  15. <tag-class>com.javatpoint.sonoo.MyTagHandler</tag-class>  
  16. </tag>  
  17. </taglib>  

3) Create the JSP file

Let's use the tag in our jsp file. Here, we are specifying the path of tld file directly. But it is recommended to use the uri name instead of full path of tld file. We will learn about uri later.
It uses taglib directive to use the tags defined in the tld file.
File: index.jsp
  1. <%@ taglib uri="WEB-INF/mytags.tld" prefix="m" %>  
  2. Current Date and Time is: <m:today/>  

Output

output of jsp custom tag example

Comments

Popular posts from this blog

What is the difference between asynchronous and synchronous execution?

Yahoo accepting requests for inactive email IDs

How to install JDK 1.7.0 and how to set path and classpath variables