Home » Custom Tags in JSP

Custom Tags in JSP

by Online Tutorials Library

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 custom tag.

Advantages of Custom Tags

The key advantages of Custom tags are as follows:

  1. Eliminates the need of scriptlet tag The custom tags eliminates the need of scriptlet tag which is considered bad programming approach in JSP.
  2. 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.
  3. Re-usability 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:


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.

Custom tags api


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.


You may also like