Simple Custom Tag Libraries In JSP

Introduction

 
The tag library consists of an Application Programming Interface (API) and simple tags, such as <jsp:invoke> and <jsp:doBody>. This API comprises of SimpleTag interface and SimpleTagSupport class. You can add dynamic attributes to tags and process the body content of tags using simple tags. Tag processing can be controlled using tag file directives. Fragments and body content can be processes using tag file actions.
 

SimpleTag

 
The SimpleTag interfaces provides doTag() method, which is supported by the classic tag handlers. The dotag() method is called whenever there is a requirement of tag invocation. All manipulations and evaluations are carried out by doTag() method. The methods present in the SimpleTag interface are,
 
doTag()
 
The method is used for handling tag processing. It retains body iteration after being processed.
 
Syntax:
  1. public void doTag() throws JspException, IOException  
For Example: Source Code
  1. import javax.servlet.jsp.tagext.*;  
  2. import javax.servlet.jsp.*;  
  3. public class Welcome extends SimpleTagSupport {  
  4.     public void doTag() thorws JspException {  
  5.         PageContext pageContext = (PageContext) getJspContext();  
  6.         JspWriter out = pageContext.getOut();  
  7.     }  
  8. }  
Code shows the implementation of the doTag() method.
 
setParent()
 
The method sets the parent of a specified tag.
 
Syntax:
  1. public void setParent(JspTag parent)  
Where the parent is the new tag that needs to be set as a parent tag.
 
Source code:
  1. public void setParent(Tag tag)  
  2. {  
  3.    parent=tag;  
  4. }  
This code shows the implementation of the setParent() method.
 
getParent()
 
The method returns the parent of the specified tag.

Syntax:
  1. public JspTag getParent()  
Source Code:
  1. public JspTag getParent()  
  2. {  
  3.    return parent;  
  4. }  
This source code shows the implementation of the getParent() method.
 
setJspContext()
 
The method sets the context to the tag handler for invocation by the container.
 
Syntax
  1. public void setJspContext(jspContext pcl)  
where pcl represents the page context for this invocation.
 
Source Code:
  1. if(a==null)  
  2. {  
  3.    out.println(“.setJspContext(pageContext);”);  
  4. }  
This code shows the implementation of the setJspContext() method.
 
setJspBody()
 
The method is provided by the body of the specified tag, which can be invoked any number of times by the tag handler.
 
Syntax:
  1. public void setJspBody(JspFragment jspBody)  
where,
jspBody is the fragment encapsulating the body of this tag.
 
Source Code:
  1. jspFragment fragment=new MockjspFragment(pageContext);  
  2. setJspBody(fragment);  
Source code shows the implementation of the setJspBody() method.
 
SimpleTagSupport Class
 
This class acts as a base class for simple tag handlers. The class implements the SimpleTag interface. It adds several useful methods. This class supports the SimpleTag interface by implementing the following methods:
 
getJspContext()
 
This method returns the context passed into the container by setJspContext() method.
 
Syntax:
  1. protected JspContext getJspContext()  
Source code:
  1. public void doTag() throws JspException {  
  2.     PageContext pageContext = (PageContext) getJspContext();  
  3.     JspWriter out = pageContext.getOut();  
  4. }  
Source code shows the implementation of the getJspContext() method.
 
getJspBody()
 
This method returns the fragment encapsulating the body of this tag, set by setJspContext()
 
Syntax:
  1. protected JspFragment getJspBody()  
Source code:
  1. ATag t1=new ATag();  
  2. JspFragment body=getJspBody();  
  3. t1.setJspBody(body);  
  4. t1.doTag();  
This code shows the implementation of the getJspBody() method.
 
Sample SimpleTag
 
The basic implementation of SimpleTag interface is managed by javax.servlet.jsp.target.SimpleTagSupport class. This interface is implemented by extending the SimpleTagSupport class and override the doTag() method.
  1. public class FindBookSimpleTag extends SimpleTagSupport {  
  2.     private String  
  3.     var;  
  4.     private static final String BOOK_TITLE = ”Learning with Advance Java”;  
  5.     public void doTag() throws JspException {  
  6.         BookBean book = new BookBean(BOOK_TITLE);  
  7.         getJspContext().setAttribute(this.var, book);  
  8.     }  
  9.     public void setVar(String  
  10.         var) {  
  11.         this.var =  
  12.             var;  
  13.     }  
  14. }  
Simple Tags API
 
The simple tags API can be understood by having a clear picture on defining empty simple tags, adding dynamic attributes in simple tags, and processing body content within simple tags.
 
Tags that do not have a body and are without attributes in the form <prefix:tagname/> are known as empty simple tags. These tags extend the TagSupport class. The doStartTag() method is to be overridden for defining a tag without attributes. This gets called at the start time when it is requested for finding the elements start tag. Print methods are to be inside the try/catch block for giving output to the screen.
 
Source Code:
  1. <%@ taglib prefix=”t” uri=”DemoTags”  %>  
  2. <html>  
  3. <head><title> Remainder Page</title></head>  
  4. <body>  
  5. <t:remainder />  
  6. </body>  
  7. </html>  
This code shows how to use a custom tag in a JSP file.
 

Adding Dynamic Attributes in Simple Tags

 
It is useful to define a tag handler accepting attributes with dynamic names that are not known until the tag is used. For example, it is not possible to anticipate what attributes a user may wish to pass to a tag that simulates an XML tag. This is can be achieved by having the tag handler class implement the javax.servlet.jsp.target.DynamicAttribute interface. This also allows declaring optional attributes for a tag.
 
This interface defines only one method setDynamicAttribute(). This method is invoked when a tag that is declared to accept dynamic attributes is passed an attribute not present in the tag library.
 
Syntax
  1. public void setDynamicAttribute(String uri, String localName, Object value) throws JspException  
Where:
  • URI is the namespace of the attribute.
  • localName is the name of the attribute.
  • value is the value of the attribute.
The setDynamicAttribute method saves the attribute names and values in the list. Then the doTag method , the names and values are echoed to the response in an HTML list first.
 
Source Code
  1. public void setDynamicAttribute(String uri, String localName, Object value) throws JspException {  
  2.     key1.add(localName);  
  3.     value1.add(value);  
  4. }  
This code shows the implementation of a tag EchoAttributeTag with an Attribute.
 

Processing Body Content within Simple Tags

 
Simple tags provide a set of steps to handle the requirement of custom tags, whenever they require access to the body content. The steps followed as below,
  1. A change is brought about in the TLD file by entering a “scriptless” value in the body context.
  2. The setjspBody() method is called by the container. By the use of getJspBody() method, fragments can be accessed and are executed using the invoke() method.
  3. Finally, with the help of out.println(), processed body content is sent to the output stream as a response.
Source Code
  1. public class Welcome extends SimpleTagSupport {  
  2.     public void doTag() thorws JspException {  
  3.         PageContext pageContext = (PageContext) getJspContext();  
  4.         JspWriter out = pageContext.getOut();  
  5.         try {  
  6.             out.println(“Hello World”);  
  7.         } catch (Exception e) {}  
  8.     }  
  9. }  

Summary

 
The SimpleTag interface provides the doTag() method which is supported by the Classic Tag Handlers. The SimpleTagSupport class acts as a base class for new simple tag handlers. The SimpleTag interface is managed by javax.servlet.jsp.tagext.SimpleTagSupport class. Tags that do not possess body or are without attributes in the form <prefix:tagname> are known as empty simple tags. The dynamic attributes are handled by javax.servlet.jsp.tagext.DynamicAttributes interface.


Similar Articles