When you want to display the dynamic content in the JSP, you can use EL Expressions and JSTL Tags.
If these two are not enough for your application requirements, then you can develop your own tags called "custom tags.
When you develop a custom tag, that can be reused across multiple JSPs.
Steps to develop a custom tag:
Identify the information related to the tag that you want to develop, like
name of the tag
body-content
attributes allowed for the tag
etc
Place one tld file in the WEB-INF or subdirectory.
Specify the tag information in the tld file which includes
URI
name of the tag
body-content
tag-class
attributes allowed for the tag
etc
Write the tag handler class with the following steps:
Define Java classes by extending TagSupport or BodyTagSupport.
Define the variables for the attributes defined for the tag.
Define the setter methods for the variables.
Override the required lifecycle methods.
Steps to use the custom tag:
Use the taglib directive to use the custom tags in JSP.
The taglib directive has two attributes called prefix and uri.
The URI of the taglib directive must match the URI specified in the TLD file.
The prefix of the taglib directive can be anything you like.
Refer to the tag in the JSP using the prefix.
Ex: <jtc:show name='Som' email='Som@jtc' phone='12345' />
Files Required For Program
1. Lab.jsp
2. jtc.tld
3. ShowTag.java
4. web.xml
1. Lab.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="jtc" uri="http://www.SomPrakashPrakash.in/myjsp/mytags" %>
<!DOCTYPE html>
<html>
<body>
<h2> I am Lab.jsp</h2>
<hr/>
<jtc:show name="SomPrakash"/>
<hr/>
<jtc:show name="SomPrakash" email="Som@jtc" phone="12345"/>
</body>
</html>
2. jtc.tld
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN""http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>mytags</short-name>
<uri>http://www.SomPrakashPrakash.in/myjsp/mytags</uri>
<description> This is my First Custom Tag </description>
<tag>
<name>show</name>
<tag-class>com.jtcindia.ctags.ShowTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>name</name>
<required>true</required>
</attribute>
<attribute>
<name>email</name>
<required>false</required>
</attribute>
<attribute>
<name>phone</name>
<required>false</required>
</attribute>
</tag>
</taglib>
3. ShowTag.java
package com.jtcindia.ctags;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
public class ShowTag extends TagSupport {
private String name;
private String email;
private String phone;
public void setName(String name) {
this.name = name;
}
public void setEmail(String email) {
this.email = email;
}
public void setPhone(String phone) {
this.phone = phone;
}
public int doEndTag() throws JspException {
String msg = "<h3>Hello " + name + " Welcome !!!</h3>";
if (email == null) {
msg = msg + " <h3>Your Email is Not Provided</h3>";
} else {
msg = msg + " <h3>Your Email is " + email + "</h3>";
}
if (phone == null) {
msg = msg + " <h3>Your Phone is Not Provided </h3>";
} else {
msg = msg + " <h3>Your Phone is " + phone + "</h3>";
}
try {
JspWriter out = pageContext.getOut();
out.print(msg);
} catch (IOException e) {
e.printStackTrace();
}
return EVAL_PAGE;
}
}
4. web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app …>
<display-name>Lab21</display-name>
<welcome-file-list>
<welcome-file>Lab.jsp</welcome-file>
</welcome-file-list>
</web-app>
Whenever a tag is encountered in the JSP, the container takes the tag name and prefix.
Container checks whether any taglib directive is available whose prefix matches the prefix of the tag encountered.
If no matching taglib directive is available, then that tag will be ignored.
If a matching taglib directive is available, then the URI of the matching taglib directive will be taken.
Container checks whether any tld file is available whose URI matches the URI of the taglib directive identified.
If a matching tld file is found, the container checks the tag description in that tld file by
checking the following:
checks whether the tag name is found in that tld or not.
If the tag name is not found, an error message will be given.
(No tag "showMessage" is defined in the tag library imported with the prefix "jtc.")
If a tag name is found, then check whether the attributes are used correctly or not.
If attribute names are not used correctly, an error message will be given:
- Undefined attribute name "phone"
- the required attribute "sname"
If attributes are used correctly, then the tag handler class will be taken.
Container starts the Tag Handler class lifecycle.
Tag Handler is the class that is responsible for processing the custom tags.
Once the Tag Handler class lifecycle is completed tag processing is completed.
Lifecycle methods of the Tag Handler class, which is extending the TagSupport class are:
void setParent(Tag)
void setPageContext(PageContext)
public int doStartTag()
public int doAfterBody()
public int doEndTag()
Lifecycle of Tag Handler class which is extending TagSupport
Files Required for Program
1. Lab.jsp
2. jtc.tld
3. IfTag.java
4. web.xml
1. Lab.jsp
<%@ taglib prefix="jtc" uri="http://www.Prakashsclasses.com/myjsp/mytags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<body>
<h2> IF Tag Lab</h2>
<hr/>
<jtc:if test="true">
<h3> Yes, I am True </h3>
</jtc:if>
<hr/>
<jtc:if test="false">
<h3> Yes, I am False </h3>
</jtc:if>
<hr/>
<c:set var="SID" value="101" scope="request"/>
<jtc:if test="${SID eq 101}">
<h3> Hello Guys , My Sid is 101 </h3>
</jtc:if>
<hr/>
<jtc:if test="${SID ne 101}">
<h3> Hello Guys , I dont know my SID </h3>
</jtc:if>
</body>
</html>
2. jtc.tld
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>mytags</short-name>
<uri>http://www.Prakashsclasses.com/myjsp/mytags</uri>
<description> This is IF Tag </description>
<tag>
<name>if</name>
<tag-class>com.jtcindia.ctags.IfTag</tag-class>
<body-content>jsp</body-content>
<attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
3. IfTag.java
package com.jtcindia.ctags;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
public class IfTag extends TagSupport {
static {
System.out.println("IfTag - S.B");
}
public IfTag() {
System.out.println("IfTag - D.C");
}
private String test; //T or F
public void setTest(String test) {
System.out.println("setTest()");
this.test = test;
}
public int doStartTag() throws JspException {
System.out.println("doStartTag()");
boolean flag = Boolean.parseBoolean(test);
if (flag) {
return EVAL_BODY_INCLUDE;
} else {
return SKIP_BODY;
}
}
public int doAfterBody() throws JspException {
System.out.println("doAfterBody()");
return SKIP_BODY;
}
public int doEndTag() throws JspException {
System.out.println("doEndTag()");
return EVAL_PAGE;
}
}
4. web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app …>
<display-name>Lab22</display-name>
<welcome-file-list>
<welcome-file>Lab.jsp</welcome-file>
</welcome-file-list>
</web-app>
You can develop the tags with and without body using TagSupport as a super class.
Using TagSupport, you can develop all types of tags, like simple tags, nested tags, tags with and without bodies, looping tags, etc.
BodyTagSupport should be used only in one specific case, i.e., if you want to get the body into your control and you want to modify the body as per the requirement, then only use BodyTagSupport as a superclass to your tag handler class.
f tag handler class is extending BodyTagSupport, then you can use the following two extra life cycle methods in the Tag Handler class:
public void setBodyContent(BodyContent bc)
public void doInitBody()
You need to return following value from doStartTag() to execute the setBodyContent()
method EVAL_BODY_BUFFERED
This is the default result from doStartTag () in the case of BodyTagSupport class.
You can use the following method to get the body content in the tag handler class
public BodyContent getBodyContent()
Lifecycle methods of Tag Handler class which is extending TagSupport class:
void setParent(Tag)
void setPageContext(PageContext)
public int doStartTag()
public void setBodyContent(BodyContent bc)
public void doInitBody()
public int doAfterBody()
public int doEndTag()
Life Cycle of the Tag Handler that extends BodyTagSupport
Files Required For Program
1. Lab.jsp
2. jtc.tld
3. UpperTag.java
4. web.xml
1. Lab.jsp
<%@ taglib prefix="jtc" uri="http://www.Prakashsclasses.com/myjsp/mytags" %>
<!DOCTYPE html>
<html>
<body>
<jtc:upper> hello huys , how are you </jtc:upper>
<hr/>
<jtc:upper> somprakash</jtc:upper>
</body>
</html>
2. jtc.tld
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>mytags</short-name>
<uri>http://www.Prakashsclasses.com/myjsp/mytags</uri>
<description> Upper Tag </description>
<tag>
<name>upper</name>
<tag-class>com.jtcindia.ctags.UpperTag</tag-class>
<body-content>jsp</body-content>
</tag>
</taglib>
3. UpperTag.java
package com.jtcindia.ctags;
import java.io.IOException;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class UpperTag extends BodyTagSupport {
BodyContent bc = null;
public int doStartTag() throws JspException {
System.out.println("doStartTag()");
return EVAL_BODY_BUFFERED;
}
public void setBodyContent(BodyContent bc) {
System.out.println("setBodyContent()");
this.bc = bc;
}
public void doInitBody() {
System.out.println("doInitBody()");
}
public int doEndTag() throws JspException {
try {
String value = bc.getString();
JspWriter out = pageContext.getOut();
out.print("<font color=blue size=4>" + value.toUpperCase() + "</font>");
} catch (IOException e) {
e.printStackTrace();
}
return EVAL_PAGE;
}
}
4. web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app …>
<display-name>Lab25</display-name>
<welcome-file-list>
<welcome-file>Lab.jsp</welcome-file>
</welcome-file-list>
</web-app>