Our own created tags are known as custom tags. By using standard tags we are able to make jsps script less. But the functionally offered by standard actions(tags) is limited. Industry strength web applications demand complex application logic. To meet that requirement and make jsps scriptles we develop custom tags.

JSP API provides library support for custom tag development through the package javax.servlet.jsp.targext. Whenever we want to develop a custom tag and its associated action, there are 2 important things we need to do.

  Ø  Writing the tag handler and copy it into the classes directory.
  Ø  Write the tab library descriptor and copy it into the WEB-INF

Tag Library descriptor:- It is an xml file with tld extension. In this file we define our own tags specify their properties and perform mappings between the tags and associated tag handlers.

Tag Handler:- Defining a tag is not sufficient to perform an action. Every tag we define should have a java class associated with it to provide custom functionality. The java class that provides the functionality for the custom tag is known as the tag handler. Every java class cannot act as a tag handler. It should be defined in compliance with the JSP Specification. According to the JSP specification, any java class can become a tag handler if it implements javax.servlet.jsp.tagext.Tag interface directly or indirectly. This interface provides life cycle methods for the tag handler. Container manages the life cycle of the tag handler and calls the life cycle methods on the tag handler instance. Tag interface provides the following important life cycle methods.

o   setPageContext(pageContext.pageContext)
o   doStartTag()
o   doEngTag()
o   release()

setPageContext():- JSP container after instantiating the tag handler calls this method by supplying the pageContext object as argument. By supplying this object, the container makes the tag handler aware of the environment in which, its associated tag runs. PageContext gives us all the implicit objects required in the tag handler.

doStartTag():-container calls this life cycle method on the tag handler instance when the start tag is encountered in the jsp. We write custom action code in this method. This method can return any of the following 2 constants to the container.

o   Tag.SKIP_BODY
o   Tag.EVAL_BODY_INCLUDE

The first contant instructs the container to ignore the body of the tag. The second constant indicates to the container that the body of the tag has to be evaluated.

doEndTag():- Container calls this method when it encounters end tag in the jsp. This method is called ireespective of whether the custom tag has been written using the full or short hand format. This method can return any of the following constants to the container.

o   Tag.SKIP_PAGE
o   Tag.EVAL_PAGE

The first constant indicates that the rest of the page after this tag should not be evaluated. The second constant indicates that the rest of the page should be evaluated. The second constant indicates that the rest of the page should be evaluated.

Release():- This method is called by the container just before the tag handler instance is garbage collected. This method is used by the programmer to release any resources(data base connections) allocated to the tag handler.

Post a Comment

 
Top