JSP Custom Tags

In this chapter, we will discuss the Custom Tags in JSP. A custom tag is a user-defined JSP language element. When a JSP page containing a custom tag is translated into a servlet, the tag is converted to operations on an object called a tag handler. The Web container then invokes those operations when the JSP pages servlet is executed.

JSP tag extensions lets you create new tags that you can insert directly into a JavaServer Page. The JSP 2.0 specification introduced the Simple Tag Handlers for writing these custom tags.

To write a custom tag, you can simply extendSimpleTagSupportclass and override thedoTag()method, where you can place your code to generate content for the tag.

Consider you want to define a custom tag named ex:Hello and you want to use it in the following fashion without a body

To create a custom JSP tag, you must first create a Java class that acts as a tag handler. Let us now create theHelloTagclass as follows

package com.tutorialspoint; import javax.servlet.jsp.tagext.*; import javax.servlet.jsp.*; import java.io.*; public class HelloTag extends SimpleTagSupport public void doTag() throws JspException, IOException JspWriter out = getJspContext().getOut(); out.println(Hello Custom Tag!);

The above code has simple coding where thedoTag()method takes the current JspContext object using thegetJspContext()method and uses it to sendHello Custom Tag!to the currentJspWriterobject

Let us compile the above class and copy it in a directory available in the environment variable CLASSPATH. Finally, create the following tag library file:Tomcat-Installation-Directorywebapps\ROOT\WEB-INF\custom.tld.

taglib tlib-version1.0/tlib-version jsp-version2.0/jsp-version short-nameExample TLD/short-name tag nameHello/name tag-classcom.tutorialspoint.HelloTag/tag-class body-contentempty/body-content /tag /taglib

Let us now use the above defined custom tagHelloin our JSP program as follows

%@ taglib prefix = ex uri = WEB-INF/custom.tld% html head titleA sample custom tag/title /head body ex:Hello/ /body /html

Call the above JSP and this should produce the following result

You can include a message in the body of the tag as you have seen with standard tags. Consider you want to define a custom tag namedex:Helloand you want to use it in the following fashion with a body

Let us make the following changes in the above tag code to process the body of the tag

package com.tutorialspoint; import javax.servlet.jsp.tagext.*; import javax.servlet.jsp.*; import java.io.*; public class HelloTag extends SimpleTagSupport StringWriter sw = new StringWriter(); public void doTag() throws JspException, IOException voke(sw); getJspContext().getOut().println(sw.toString());

Here, the output resulting from the invocation is first captured into aStringWriterbefore being written to the JspWriter associated with the tag. We need to change TLD file as follows

taglib tlib-version1.0/tlib-version jsp-version2.0/jsp-version short-nameExample TLD with Body/short-name tag nameHello/name tag-classcom.tutorialspoint.HelloTag/tag-class body-contentscriptless/body-content /tag /taglib

Let us now call the above tag with proper body as follows

%@ taglib prefix = ex uri = WEB-INF/custom.tld% html head titleA sample custom tag/title /head body ex:Hello This is message body /ex:Hello /body /html

You will receive the following result

You can use various attributes along with your custom tags. To accept an attribute value, a custom tag class needs to implement thesettermethods, identical to the JavaBean setter methods as shown below

package com.tutorialspoint; import javax.servlet.jsp.tagext.*; import javax.servlet.jsp.*; import java.io.*; public class HelloTag extends SimpleTagSupport private String message; public void setMessage(String msg) ssage = msg; StringWriter sw = new StringWriter(); public void doTag() throws JspException, IOException if (message != null) /* Use message from attribute */ JspWriter out = getJspContext().getOut(); out.println( message ); else /* use message from the body */ getJspBody().invoke(sw); getJspContext().getOut().println(sw.toString());

The attributes name ismessage, so the setter method issetMessage(). Let us now add this attribute in the TLD file using theattributeelement as follows

taglib tlib-version1.0/tlib-version jsp-version2.0/jsp-version short-nameExample TLD with Body/short-name tag nameHello/name tag-classcom.tutorialspoint.HelloTag/tag-class body-contentscriptless/body-content attribute namemessage/name /attribute /tag /taglib

Let us follow JSP with message attribute as follows

%@ taglib prefix = ex uri = WEB-INF/custom.tld% html head titleA sample custom tag/title /head body ex:Hello message = This is custom tag / /body /html

This will produce following result

Consider including the following properties for an attribute

The name element defines the name of an attribute. Each attribute name must be unique for a particular tag.

This specifies if this attribute is required or is an optional one. It would be false for optional.

Declares if a runtime expression value for a tag attribute is valid

Defines the Java class-type of this attribute. By default it is assumed asString

Informational description can be provided.

Declares if this attribute value should be treated as aJspFragment.

Following is the example to specify properties related to an attribute

….. attribute nameattribute_name/name requiredfalse/required typejava.util.Date/type fragmentfalse/fragment /attribute …..

If you are using two attributes, then you can modify your TLD as follows

….. attribute nameattribute_name1/name requiredfalse/required typejava.util.Boolean/type fragmentfalse/fragment /attribute attribute nameattribute_name2/name requiredtrue/required typejava.util.Date/type /attribute …..