Custom Tags in JSP:
Custom tags are user-defined tags. They eliminates the possibility of scriptlet
tag and separates the business logic from the JSP page.
The same business logic can be used many times by the use of costom tag.
Advantages of Custom Tags:
The key advantages of Custom tags are as follows:
Eliminates the need of srciptlet tag The custom tags eliminates the need of
scriptlet tag which is considered bad programming approach in JSP.
Separation of business logic from JSP The custom tags separate the the
business logic from the JSP page so that it may be easy to maintain.
Reusability The custom tags makes the possibility to reuse the same
business logic again and again.
Syntax to use custom tag
There are two ways to use the custom tag. They are given below:
<prefix:tagname attr1=value1....attrn=valuen /> |
<prefix:tagname attr1=value1....attrn=valuen >
body code
</prefix:tagname>
JSP Custom Tag API
The javax.servlet.jsp.tagext package contains classes and interfaces for
JSP custom tag API. The JspTag is the root interface in the Custom Tag
hierarchy.
JspTag interface
The JspTag is the root interface for all the interfaces and classes used in
custom tag. It is a marker interface.
Tag interface
The Tag interface is the sub interface of JspTag interface. It provides
methods to perform action at the start and end of the tag.
Fields of Tag interface
There are four fields defined in the Tag interface. They are:
Field Name | Description |
public static int EVAL_BODY_INCLUDE | it evaluates the body content. |
public static int EVAL_PAGE | it evaluates the JSP page content after the custom tag. |
public static int SKIP_BODY | it skips the body content of the tag. |
public static int SKIP_PAGE | it skips the JSP page content after the
custom tag. Methods of Tag interface |
The methods of the Tag interface are as follows:
Method Name | Description |
public void setPageContext(PageContext pc) | it sets the given PageContext object. |
public void setParent(Tag t) | it sets the parent of the tag handler. |
public Tag getParent() | it returns the parent tag handler
object. |
public int doStartTag()throws JspException | it is invoked by the JSP page
implementation object. The JSP programmer should override this method and
define the business logic to be performed at the start of the tag. |
public int doEndTag()throws JspException | it is invoked by the JSP page
implementation object. The JSP programmer should override this method and
define the business logic to be performed at the end of the tag. |
public void release() | it is invoked by the JSP page implementation object to release the state. |
IterationTag interface
The IterationTag interface is the sub interface of the Tag interface. It
provides an additional method to reevaluate the body.
Field of IterationTag interface
There is only one field defined in the IterationTag interface.
public static int EVAL_BODY_AGAIN it reevaluates the body content.
Method of Tag interface
There is only one method defined in the IterationTag interface.
public int doAfterBody()throws JspException it is invoked by the JSP page
implementation object after the evaluation of the body. If this method returns
EVAL_BODY_INCLUDE, body content will be reevaluated, if it returns
SKIP_BODY, no more body cotent will be evaluated.
TagSupport class
The TagSupport class implements the IterationTag interface. It acts as
the base class for new Tag Handlers. It provides some additional methods also.
Understanding Flow and Example of JSP Custom Tag
There is given two simple examples of JSP custom tag. One example of JSP custom
tag, performs action at the start of the tag and second example performs action
at the start and end of the tag.
Attributes in Custom Tag
Here, we will learn how we can define attributes for the custom tag.
Iteration using Custom Tag
In this example, we are iterating the body content of the custom tag.
Custom URI in Custom Tag
We may also refer the TLD file by using the URI. Here we will learn how can we
use custom URI.
Example: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:
Create the Tag handler class and perform action at the start or at the
end of the tag.
Create the Tag Library Descriptor (TLD) file and define tags
Create the JSP file that uses the Custom tag defined in the TLD file
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:
package ycd.com; import java.util.Calendar; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; public class MyTagHandler extends TagSupport{ public int doStartTag() throws JspException { JspWriter out=pageContext.getOut();//returns the instance of JspWriter try{ out.print(Calendar.getInstance().getTime());//printing date and time using JspWriter }catch(Exception e){System.out.println(e);} return SKIP_BODY;//will not evaluate the body content of the tag } }
|
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
<?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/j2ee/dtd/web-jsptaglibrary_1_2.dtd"> <taglib> <tlib-version>1.0</tlib-version> <jsp-version>1.2</jsp-version> <short-name>simple</short-name> <uri>http://tomcat.apache.org/example-taglib</uri> <tag> <name>today</name> <tag-class>ycd.com.MyTagHandler</tag-class> </tag> </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 <%@ taglib uri="WEB-INF/mytags.tld" prefix="m" %> Current Date and Time is: <m:today/> |