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:
- 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.
- 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:
body code
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.
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.
Leave A Comment