we have seen that how to create the custom JSP tag using interface “Tag”, means tags which does not have a body. In this article, I will explain creating custom Tags which have a body. We can manipulate the content of body as we want.
This article is about creating custom Tag in JSP using “Tag” interface.
Creating custom tag in JSP has its unique advantages. This is same as Custom controls of the ASP.Net.
By creating the Custom tag, we can resuse that tag every where in our application. Best example of custom tags are the Struts.
Types of Tag:
Tag without body:
<ct:showTime displayTime="false" />
Tag with body:
<ct:showTime displayTime="false" >
Current Time is :
As ther are two types of tag, there are two way of creating them in JSP:
By implementing below interfaces:
Or by extending any of the two below class (provides default implementation of interfaces discussed above):
TagSupport (internally implements Tag)
BodyTagSupport (internally implements BodyTag)
In this article, I will use the interface approach, so that the beginners can get broader idea about how Tag library actually implemented.
The interface of a classic tag handler that does not want to manipulate its body. The Tag interface defines the basic protocol between a Tag handler and JSP page implementation class.
The Tag interface specifies the setter and getter methods for the core pageContext and parent properties.
There are two main actions: doStartTag and doEndTag. Once all appropriate properties have been initialized, the doStartTag and doEndTag methods can be invoked on the tag handler. Between these invocations, the tag handler is assumed to hold a state that must be preserved. After the doEndTag invocation, the tag handler is available for further invocations (and it is expected to have retained its properties).
Empty and Non-Empty Action:
If the TagLibraryDescriptor file indicates that the action must always have an empty action, by an entry of “empty”, then the doStartTag() method must return SKIP_BODY.
Otherwise, the doStartTag() method may return SKIP_BODY or EVAL_BODY_INCLUDE.
If SKIP_BODY is returned the body, if present, is not evaluated.
If EVAL_BODY_INCLUDE is returned, the body is evaluated and “passed through” to the current out.