In this program, We will create a Custom Tag which changes the background color.
JSP Custom Tags :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.
Modifying the response generated by a calling JSP page.
The various types of custom tags that you can develop in JSP are :
<td:welcome />
<td: welcome color=?blue?></td:welcome>
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. The following code snippet shows a custom tag that contains a JSP
scripting element as its body:
<td: welcome>
<%=today_date%>
</td:welcome>
Nested tags: Refer to the set of custom tags in which one custom tag encloses one or more custom tags. The following code snippet shows a nested custom tag:
<td1:ifTag condition ?<%=eval>? >
<td2:valueTrue>
The expression evaluates to true
</td2:valueTrue>
</td1:ifTag>
Development of Custom Tag :
To develop a custom tag, you need to perform following steps:
Develop a tag handler or class file(.java)
Develop the Tag Library Descriptor (TLD) file
Include the Tag Library in a JSP page
EXAMPLE : In this Example, We will create a Custom Tag which changes the background color .When you enter your Name in Html form & select color -color changes according to your choice, this background color change is due to the custom tag, which we have created with the help of tag handler class :-
Tag Handler File :
Build a class that implements the
javax.servlet.jsp.tagext tag interface as follows. Compile it and place it
under the web-inf/classes directory (in the appropriate package structure).
package hello; |
Tag Library Descriptor (TLD) file : Now we need to describe the tag, so create a file called Mytaglib.tld and place it under the web-inf directory.
<taglib> |
HTML File :
AcceptInput.html
<html> |
JSP File :
helloCustomTag.jsp
<%-- Providing the location of the Mytaglib directive --%> |
Deployment of application (OUTPUT) :

After Submitting the values :
