Custom Tags in JSP

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.

The key advantages of Custom tags are as follows:

Eliminates the need of scriptlet 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.

The custom tags makes the possibility to reuse the same business logic again and again.

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

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.

The JspTag is the root interface for all the interfaces and classes used in custom tag. It is a marker 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.

There are four fields defined in the Tag interface. They are:

public static int EVAL_BODY_INCLUDE

it evaluates the JSP page content after the custom tag.

it skips the body content of the tag.

it skips the JSP page content after the custom tag.

The methods of the Tag interface are as follows:

public void setPageContext(PageContext pc)

it sets the given PageContext object.

it sets the parent of the tag handler.

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.

it is invoked by the JSP page implementation object to release the state.

The IterationTag interface is the sub interface of the Tag interface. It provides an additional method to reevaluate the body.

There is only one field defined in the IterationTag 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.

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.

Here, we will learn how we can define attributes for the custom tag.

In this example, we are iterating the body content of the custom tag.

We may also refer the TLD file by using the URI. Here we will learn how can we use custom URI.

JavaTpoint offers too many high quality services. Mail us on[emailprotected], to get more information about given services.

JavaTpoint offers college campus training on Core Java, Advance Java, , Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at[emailprotected]