Custom Tag Overview

A custom tag is a user-defined JSP language element. When a JSP page containing a custom tag is translated into a servlet, the tag is converted to operations on a tag handler. The web container then invokes those operations when the JSP page’s servlet is executed.
Features of custom tag :
  • Attributes can be passed from the jsp page.
  • Pass variables back to the calling page.
  • Access all the objects available to JSP pages.
  • Custom tag can be nested.
  • Error handing in custom tag.
Custom Tag API
Custom Tag Directory Structure
custom overview
JspTag Interface
public interface JspTag
JspTag interface serves as a base class for all interfaces and classes which are used to create custom tag for JSP pages. It is a marker interface. This type of interface is used for organizational and type-safety purposes.
Tag Interface
public interface Tag extends JspTag
The Tag interface extends JspTag interface. The Tag interface defines the basic protocol between a Tag handler and JSP page implementation class.This interface has four static int variable and six method to defines the life cycle of tag handler. These methods are used to start and end tag.
Static variable of Tag Interface
  1. public static int EVAL_BODY_INCLUDE : This variable instructs the JSP engine to evaluate both the tag's body content and any child tags it has.
  2. public static int SKIP_BODY : This variable instructs the JSP engine to ignore the body content.
  3. public static int EVAL_PAGE : This variable instructs the JSP engine to evaluate the rest of the page.
  4. public static int SKIP_PAGE : This variable instructs the JSP engine to terminate evaluation ot the page.
Method of Tag Interface
  1. public void setPageContext(PageContext pc) : This method is an initilization method that makes the PageContext of the JSP available to the tag.
  2. public void setParent(Tag t) : It is used to set the parent of the tag handler.
  3. public Tag getParent() : It is used to return the parent tag handler object.
  4. public int doStartTag() throws JspException : This is called after the tag has been initilized, when the JSP engine encounters the opening of a tag at run time. Its return value should be one of two constants definined in the Tag interface: EVAL_BODY_INCLUDE, and SKIP_BODY.
  5. public int doEndTag() throws JspException : This is called when the JSP engine encounters the closing of a tag of an element at run time. Its return value should be one of two constants definined in the Tag interface: EVAL_PAGE, and SKIP_PAGE.
  6. public void release() : This method is call to the tag handler to release any resources, such as to close the JDBC connection or open a socket that the handler requeirs for its function.
IterationTag Interface
public interface IterationTag extends Tag
IterationTag extends Tag interface. It provides a single new method that allows the tag to repeat the evaluation of the tag body content.
static variable of IterationTag Interface
  • public static int EVAL_BODY_AGAIN : This static variable is used to request the reevaluation of some body.
Method of IterationTag Interface
  • public int doAfterBody()throws JspException : This method is called each time the tag's body has been processed. The valid return value is EVAL_BODY_INCLUDE, and SKIP_BODY.
BodyTag Interface
public interface BodyTag extends IterationTag
The BodyTag interface extends IterationTag. It is the responsibility of the tag handler to manipulate the body content. For example the tag handler may take the body content, convert it into a String using the bodyContent.getString method and then use it. Or the tag handler may take the body content and write it out into its enclosing JspWriter using the bodyContent.writeOut method.
static variable of BodyTag Interface
  1. public static int EVAL_BODY_TAG : This static variable is deprecated constant that has the same value as EVAL_BODY_BUFFERED and EVAL_BODY_AGAIN.
  2. public static int EVAL_BODY_BUFFERED : This static variable requests the creation of new buffer, a BodyContent on which to evaluate the body of this tag. Returned from doStartTag when it implements BodyTag. This is an illegal return value for doStartTag when the class does not implement BodyTag.
Method of BodyTag Interface
  1. public void setBodyContent(BodyContent bc) : This initilization method is used to set the object used to manipulate body content. It is only invoked if doStartTag() returned EVAL_BODY_BUFFERED.
  2. public void doInitBody()throws JspException : This method prepares for evaluation of the body. This method is invoked by the JSP page implementation object after setBodyContent and before the first time the body is to be evaluated.
TagSupport class
public class TagSupport extends Object implements IterationTag,Serializable
TagSupport class implemnts IterationTag interface .The TagSupport class is a utility class intended to be used as the base class for new tag handlers.
Many tag handlers will extend TagSupport and only redefine a few methods.
static variable of TagSupport Interface
  1. protected String id :
  2. protected PageContext pageContext :
Method of TagSupport Interface
  1. public static final Tag findAncestorWithClass(Tag t, Class c) : This method is used to find the instance of a given class type that is closest to a given instance.
  2. public int doStartTag() throws JspException : This is called after the tag has been initilized, when the JSP engine encounters the opening of a tag at run time. Its return value should be one of two constants definined in the Tag interface: EVAL_BODY_INCLUDE, and SKIP_BODY.
  3. public int doEndTag() throws JspException :This is called when the JSP engine encounters the closing of a tag of an element at run time. Its return value should be one of two constants definined in the Tag interface: EVAL_PAGE, and SKIP_PAGE.
  4. public int doAfterBody() throws JspException : This method is called each time the tag's body has been processed. The valid return value is EVAL_BODY_INCLUDE, and SKIP_BODY.
  5. public void release() : This method is call to the tag handler to release any resources, such as to close the JDBC connection or open a socket that the handler requeirs for its function.
  6. public void setParent(Tag t) : It is used to set the parent of the tag handler.
  7. public Tag getParent() : It is used to return the parent tag handler object.
  8. public void setId(java.lang.String) : This method is used to set the id attribute for this tag.
  9. public String getId() : This method is used to return the value of the id attribute of this tag; or null.
  10. public void setPageContext(PageContext pc) : This method is an initilization method that makes the PageContext of the JSP available to the tag.
  11. public void setValue(String str, Object obj) : This method is used to set a value with a String key.
  12. public Object getValue(String str) :This method is used to get a the value associated with a key.
  13. public void removeValue(java.lang.String) : This method is used to remove a value associated with a key.
  14. public Enumeration getValues() :  This method is used get enumerate the values kept by this tag handler.
 



 

Latest Tutorial


Most Visited Tutorial