Custom Tags Using JSP [ Implementing View Helper Design Pattern ]
- What is JSP Custom Tags?
- Overview of Tags
- Benefits of Custom Tags
- How to Develop a Custom Tag
- Developing a Tag Handler
- Developing the TLD File
- Including the Tag Library in a JSP
- Including Tab Library in web.xml
- Custom Tag Examples
|What is JSP Custom Tags?|
JSP Custom Tags
- 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 an object called a tag handler. and
- The Web container then invokes those operations when the JSP page’s servlet is executed.
Custom tags have a rich set of features. They can
- Be customized via attributes passed from the calling page.
- Access all the objects available to JSP pages.
- Modify the response generated by the calling page.
- Communicate with each other. You can create and initialize a JavaBeans component, create a variable that refers to that bean in one tag, and then use the bean in another tag.
- Be nested within one another, allowing for complex interactions within a JSP page
- JSP technology also provides a mechanism for encapsulating other types of dynamic functionality in custom tags, which are extensions to the JSP language.
- Provide a mechanism to a Web programmer to reuse and encapsulate complex recurring code in a JSP application.
- Provide simplicity and reusability of Java code.
- Enable you to perform various functions, such as:
- Accessing all implicit variables of a JSP page, such as request, response, in, and out.
- Initializing and instantiating a JavaBean component.
|Overview of Tags|
The various types of custom tags that you can develop in JSP are:
Refer to the custom tags that do not have any attribute or body.
Tags with attributes
Refer to custom tags for which you can define attributes to customize the behavior of the custom tag.
<td: Bye color=blue></td:Bye>
Tags with a body
Refer to the custom tag within which you can define nested custom tags, scripting elements, actions, HTML text, and JSP directives.
<td: Hello> <%=UserName%> </td:Hello>
Refer to the set of custom tags in which one custom tag encloses one or more custom tags.
<td1:ifTag condition <%=eval>> <td2:valueTrue> The expression evaluates to true </td2:valueTrue> </td1:ifTag>
|Benefits of Custom Tags|
A very important thing to note about JSP custom tags is that they do not offer more functionality than scriptlets, they simply provide better packaging, by helping you improve the separation of business logic and presentation logic.
Some of the benefits of custom tags are:
- They can reduce or eliminate scriptlets in your JSP applications. Any necessary parameters to the tag can be passed as attributes or body content, and therefore no Java code is needed to initialize or set component properties.
- They have simpler syntax. Scriptlets are written in Java, but custom tags can be used in an HTML-like syntax.
- They can improve the productivity of non programmer content developers, by allowing them to perform tasks that cannot be done with HTML.
- They are reusable. They save development and testing time. Scritplets are not reusable, unless you call cut-and-paste reuse.
- In short, you can use custom tags to accomplish complex tasks the same way you use HTML to create a presentation.
|How to Develop a Custom Tag?|
To develop a custom tag, you need to perform following steps
- Develop a tag handler
- Develop the Tag Library Descriptor (TLD) file
- Include the Tag Library in a JSP page
- Deploy the application
|Developing a Tag Handler|
- All custom tags have a corresponding tag handler, which is a Java class that implements the functionality of the custom tag.
- The javax.servlet.jsp.tagext package provides the classes and interfaces that you can use to develop tag handlers.
- Base classes, such as TagSupport and BodyTagSupport of the javax.servlet.jsp.tagext package implements the Tag interface to provide implementation of the interface methods.
- You can extend these helper classes in your tag handler classes and override those methods that are required to implement the functionality of your tag.
- You can extend the TagSupport class of the javax.servlet.jsp.tagext package in your tag handler to develop a tag handler for an empty tag.
The methods that you need to override in the tag handler of an empty custom tag are:
- doStartTag() :- Is called when the container encounters the start tag of a custom tag.
- doEndTag() :- Is called when the container encounters the end tag of a custom tag.
The following code shows a tag handler, Hello that extends the TagSupport class to implement a custom tag:
Note:-For Compilation of Tag Handler class do the following
C:\>set classpath=C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\jsp-api.jar;.; C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\JSPEmptyCustomTag\WEB-INF\src>javac -d . Hello.java
|Developing the TLD File|
A TLD file
- Defines a custom tag in XML format.
- Provides information, such as tag library version, name of the tag, description of the tag, and the name of the tag handler that implements the tag.
- Contains a root element <taglib> within which various elements appear.
- Defines multiple custom tag using a <tag> element within <taglib> element.
The following code snippet shows a TLD file that defines an empty tag, Hello implemented by the Hello tag handler.
Code of mytaglib.tld file
<body-content>JSP</body-content> - for container tag OR Body tag <body-content>empty</body-content> - for empty tag
|Including the Tag Library in a JSP|
The taglib Directive
- Imports a custom tag into the current JSP page.
- Associates itself with a URI to uniquely identify a custom tag.
- Associates a tag prefix string that distinguishes a custom tag with the other tag library used in a JSP page.
- The taglib directive declares that the JSP page uses custom tags, names the tag library that defines them, and specifies their tag prefix.
- You must use a taglib directive before you use the custom tag in a JSP page.
- You can use more than one taglib directive in a JSP page, but the prefix defined in each must be unique.
<%@ taglib uri="tag_lib_URI" prefix="prefix" %>
<%@ taglib uri="hello.tld" prefix="x" %> <x:DispCust>James</x:DispCust>
Including the Tag Library in a JSP
- The <taglib> directive allows you to include a tag library in a JSP page.
- The <taglib> directive contains the uri attribute that specifies the location of the TLD file and a prefix attribute that specifies the name with which the JSP page will use the custom tags.
Test the Tag
<%@ taglib uri="mytaglib.tld" prefix="mytag"%> <mytag:Hello />
|Including Tab Library in web.xml|
We can configure the web.xml deployment descriptor with the <jsp-config> element, as below:
<jsp-config> <taglib> <taglib-uri>/Hello.tld<taglib-uri> <taglib-location> /alltag/Welcome.tld<taglib-location> </taglib> </jsp-config>
|Custom Tag Examples|