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 color=?blue?></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>? >
The expression evaluates to true
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).
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.
HTML File :
JSP File :
Deployment of application (OUTPUT) :
After Submitting the values :
Recommend the tutorial