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.
The key benefits of Custom tags are as follows:
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>
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.
The JspTag is the basic interface for all of the interfaces and training utilized in the custom tags. It is a marker 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.
There are four fields described withinside the Tag interface. They are:
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.
The methods of Tag Interface are given below:
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
The IterationTag interface is the sub-interface of the Tag interface. It offers a further technique to reevaluate the body.
There is the simplest field described withinside the IterationTag interface.
There is best one approach described withinside the IterationTag interface.
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.
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.
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 to the TLD file by using the URI. Here we will learn how can we use custom URI