JSP - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Custom Tags in JSP

Custom Tags in JSP

shape Description

Custom Tags in JSP are user defined tags and are used to separate business logic from JSP pages and eliminate scriptlet tag. By using these custom tags one can use same business logic many times. JSP page will be translated into servlet if it has custom tags, the tag handler is an instance used to translate user defined tags to operations. After the execution of JSP servlet page operations will be invoked by the web container. The following are the advantages of custom tags.

shape Syntax

<prefix:tagname attr1=value1....attrn=valuen /> //In two ways custom tag syntax can be written <prefix:tagname attr1=value1....attrn=valuen > body code  </prefix:tagname>

JSP Custom Tag API

shape Conceptual figure

Custom Tags in JSP - Following is the JSP tag evaluation.

JSPTag interface

shape Description

The JSPTag is utilized as a part of custom tags. JSPTag interface is marker interface and is the hub interface for interface and classes. The Tag interface is sub-interface and gives strategies to implement an operation. Following are the fields and methods for tag interface.

Fields of Tag interface

Field Name Description
public static int SKIP_PAGE After custom tag it avoids information of JSP page .
public static int SKIP_BODY To vanish the tag body information.
public static int EVAL_PAGE It access JSP page information.
public static int EVAL_BODY_INCLUDE It access information of the body.

Methods of Tag interface:

Methods Purpose
public void setparent  Tag handler parent will be instantiated.
public void setpagecontext(pagecontext pagecontext) Set the pagecontext object.
public int dostartTag() throws jspException JSP programmer should override this method and write the business logics to be performed at the start of the tag.
public void release() To discharge the state open void discharge() is summoned by the JSP execution object.
public int doEndTg() throws jspException JSP developer ought to abrogate this method and compose the business logic to be performed toward the end of the tag..
public Tag getTag() Gets the parent tag handler object.

Iteration Tag interface

shape Description

Custom Tags in JSP - The Iteration Tag interface is also a sub interface of Tag interface and it gives some methods. The TagSupport class actualizes the Iteration Tag interface. It as fundamental class of the new Tag Handlers.

Custom Tag

shape Example

Following is an example for Custom Tags in JSP. Following are the steps to be performed while working with custom tags. custom.jsp [java] <%@ taglib uri="WEB-INF/mytags.tld" prefix="m" %> Current Date and Time is: <m:today/> [/java] Here tagname will implement tag from taglib and the prefix which is given in the taglib library. mytags.tld [xml] <?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>MyTagHandler.MyTagHandler</tag-class> </tag> </taglib> [/xml]  web.xml [xml] ?xml version="1.0" encoding="UTF-8"? <web-app> <welcome-file-list> <welcome-file>custom.jsp</welcome-file> </welcome-file-list> </web-app> [/xml] Here page will be compiled from the custom.jsp .  MyTagHandler.java [java] 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 } } [/java] SKIP_BODY is an optional returned value but this value must be returned by doStartTag() when you want to skip the body evaluation that is it must be returned when the TagLibraryDescriptor file contains the element empty, the value “empty” shows that there will always be an empty action. The java.util.Calendar.getInstance() method gets a calendar using the specified time zone and specified locale. Output

Summary

shape Key Points

  • Custom Tags in JSP - The primary advantage of custom tag is Re-usability.
  • Custom tags are used to divide the business logic from JSP file.