You are on page 1of 21

JSP CUSTOM TAG

Creating custom tags


Custom tags are user-defined action tags that can be used
empty custom tag(without body)
within Java Server Pages. A tag handler is associated with
each tag to implement the operations. Therefore, it separates •<prefix : suffix attribute = “value”/>
the business logic from JSP and helps avoid the use of Non-empty custom tag
scriptlet tags. The scriptlet tag embeds java code inside the •<prefix : suffix attribute = “value”>body</prefix : suffix>
JSP page itself rendering the page difficult to understand
therefore, it is better to avoid the use of scriptlet.
The following three components are required to develop
custom tags :
A custom tag is a user-defined JSP language element. When 1.Tag Handler
a JSP page containing a custom tag is translated into a 2.TLD(Tag Library Descriptor) file
servlet, the tag is converted to operations on an object called 3.Taglib directive in jsp file
a tag handler. The Web container then invokes those
operations when the JSP page's 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
extend SimpleTagSupport class and override
the doTag() method, where you can place your code to
generate content for the tag.
Tag Handler
It is a container-managed object created by a JSP container at runtime. A
Java class is used to implement the processing logic for the tag. It may
contain certain properties corresponding to the attributes or body of the tag.
All tag handlers have a pageContext property for the JSP page where the tag
is located, and a parent property for the tag handler to the closest enclosing
parent tag. It must implement/extend one of the following interfaces/classes
present in javax.servlet.jsp.tagext package :

Lifecycle of tag handler:

1. During runtime, the JSP container is responsible to create the object of


implementing the tag handler class using the no-arg constructor.
2. Once the object is created, the JSP container calls the setParent and
setPageContext methods on the tag handler
3. JSP container then calls setter methods on the tag handler to set its
properties using the values of corresponding attributes of the tag.
4. When the object is ready for use, various lifecycle methods are called on
the object.
5. Pooling of tag handlers is managed by the JSP container. The same tag
handler might be used for multiple occurrences of tag on the page if the
values of the attributes are the same. But if the values of attributes are
different, properties might need to be reset. In the case of SimpleTag and
SimpleTagSupport classes, tag handlers are not pooled.
Tag Interface
Tag interface can be implemented by a tag handler
<taglib> Let us now use the above defined custom tag Hello in our
to provide the processing logic if the body of the
<tlib-version>1.0</tlib-version> JSP program as follows −
tag is not required to be evaluated. It provides the
<jsp-version>2.0</jsp-version>
following methods :
<short-name>Example TLD</short-name> <%@ taglib prefix = "ex" uri =
1. int doEndTag() "WEB-INF/custom.tld"%>
2. int doStartTag() <tag> <html>
3. Tag getParent() <name>Hello</name> <head>
4. void release() <tag- <title>A sample custom tag</title>
5. void setPageContext(PageContext pc) class>com.tutorialspoint.HelloTag</tag- </head>
6. void setParent(Tag t) class> <body>
<body-content>empty</body-content> <ex:Hello/>
</tag> </body>
package com.tutorial;
</taglib> </html>
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*; Let us compile the above class and copy it
import java.io.*; in a directory available in the environment Call the above JSP and this should produce the following
variable CLASSPATH. Finally, create the result −
public class HelloTag extends SimpleTagSupport { following tag library file: <Tomcat-
public void doTag() throws JspException, Installation-Directory>webapps\ROOT\ Hello Custom Tag!
IOException { WEB-INF\custom.tld.
JspWriter out = getJspContext().getOut();
out.println("Hello Custom Tag!");
}
}
The above code has simple coding where the doTag()
method takes the current JspContext object using the
getJspContext() method and uses it to send "Hello
Custom Tag!" to the current JspWriter object
Alt+insert to override methods
Select doStartTag()
Select Tag Library Descriptor

You might also like