logo

JSP Custom Tags


Show

Custom tags are user-defined tags. They remove the possibility of a scriptlet tag and separates the business logic from the JSP web page.

The identical business logic may be used frequently through the usage of custom tags.

Advantages of Custom Tags

The key benefits of Custom tags are as follows:

  • Eliminates the want of scriptlet tag The custom tags remove the want of scriptlet tag that's taken into consideration horrific programming methods in JSP.
  • Separation of business logic from JSP The custom tags separate the business logic from the JSP web page in order that it is able to be easy to maintain.
  • Re-usability The custom tags makes the opportunity to reuse the identical business logic once more and once more.

Syntax to Apply Custom Tag

There are methods to apply 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 deal includes training and interfaces for JSP custom tag API. The JspTag is the basic interface withinside the Custom Tag hierarchy.

JSP Tag interface

The JspTag is the basic interface for all of the interfaces and training utilized in the custom tags. It is a marker interface.

Tag Interface

The Tag interface is the sub-interface of the JspTag interface. It offers techniques to carry out movement at the beginning and end of the tag.

Fields of Tag interface

There are four fields described withinside the Tag interface. They are:

Field Name

Description

public static int EVAL_BODY_INCLUDE

it checks the body content.

public static int EVAL_PAGE

it checks the JSP page content after the custom tag.

public static int SKIP_BODY

it leaves out the body content of the tag.

public static int SKIP_PAGE

it leaves out the JSP page content after the custom tag.

Methods of Tag Interface

The methods of Tag Interface are given below:

Method Name

Description

public void setPageContext(PageContext pc)

it sets the mentioned PageContext object.

public void setParent(Tag t)

it sets the parent of the tag handler.

public Tag getParent()

it gives back the parent tag handler object.

public int doStartTag()throws JspException

it is invoked by the JSP page implementation object. The JSP programmer must override this way and explain the business logic to be performed at the beginning of the tag.

public int doEndTag()throws JspException

it is supplicated by the JSP page implementation object. The JSP programmer must override this way and explain the business logic to be performed in the last of the tag.

public void release()

it is invoked by the JSP page implementation object to liberate the state


Iteration Tag Interface

The IterationTag interface is the sub-interface of the Tag interface. It offers a further technique to reevaluate the body.

Field of IterationTag interface

There is the simplest field described withinside the IterationTag interface.

  • public static int EVAL_BODY_AGAIN it reevaluates the body content.

Method of Tag interface

There is best one approach described withinside the IterationTag interface.

  • public int doAfterBody()throws JspException it's far invoked through the JSP page implementation item after the evaluation of the body. If this technique returns EVAL_BODY_INCLUDE, body content material might be reevaluated, if it returns SKIP_BODY, no more body content might be evaluated.

TagSupport class

The TagSupport class implements the IterationTag interface. It acts as the base class for brand new Tag Handlers. It gives a few additional techniques also.

Understanding Flow and Example of JSP Custom Tag

There are given two simple examples of JSP custom tags. One example of JSP custom tag performs the action at the start of the tag, and the second example performs the 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 to the TLD file by using the URI. Here we will learn how can we use custom URI