SlideShare a Scribd company logo
CUSTOM TAG LIBRARY
Prof. AshishSingh Bhatia
1Prof. AshishSingh Bhatia
Two Approach
 Java Based Custom Tag
 No Version Restriction
 Uses Java file for Tag Handling
 Preferred when lot of java code is
required for getting output
 JSP Based Custom Tag
 Only from JSP 2.0
 Uses JSP file for Tag Handling
 Preferred when lot of html code is
required for getting output.
Prof. AshishSingh Bhatia 2
Tag Library Components
 Tag handler class that defines the tag behavior.
 The TLD file that maps the XML element names to the tag implementation.
 The JSP file that uses the tag library.
3Prof. AshishSingh Bhatia
The Tag Handler Class
 Class that tells what to do when system see the tag.
 Class must implement SimpleTag interface.
 In practice, extends SimpleTagSupport which implements SimpleTag.
 javax.servlet.jsp.tagext package.
 Every Tag Handler class must have 0 argument constructor.
 doTag() is the main method for tag handling.
 We need JspWriter [ getJspContext().getOut() ]
 New instance is created for every tag occurrence on the page.
Prof. AshishSingh Bhatia 4
Directory Structure
hellowordtag
index.jsp
WEB-INF
tlds tag.tld
classes mytag HelloWorldTag.java
Prof. AshishSingh Bhatia 5
Tag Handler Class
package mytag;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
public class HelloWorldTag extends SimpleTagSupport {
public void doTag() throws JspException, IOException {
JspWriter out = getJspContext().getOut();
out.print("<b>Hello World</b>");
}
}
Prof. AshishSingh Bhatia 6
TLD : Tag Library Descriptor
<?xml version="1.0" ?>
<taglib version="2.0">
<tlib-version>1.0</tlib-version>
<short-name>mytag</short-name>
<tag>
<description>HelloWorld Tag</description>
<name>helloworld</name>
<tag-class>mytag.HelloWorldTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
Prof. AshishSingh Bhatia 7
Optional
Required Element
empty, scriptless, tagdependent, JSP
JSP File
<html>
<head>
<title>Tag Example</title>
</head>
<body>
<%@ taglib uri="/WEB-INF/tlds/tag.tld" prefix="ashish" %>
<ashish:helloworld/>
</body>
</html>
Prof. AshishSingh Bhatia 8
Assigning Attributes to Tags
 For every attribute we need a set method in tag handler class
 <prefix:tag attribute1=“value1” attribute2=“value2” … />
Prof. AshishSingh Bhatia 9
setAttribute1(String value) setAttribute2(String value)
TLD File
<attribute>
<description>…. </description>
<name> X </name>
<required> true/false </required>
</attribute>
Prof. AshishSingh Bhatia 10
X = Must match with the
variable in class
Example : Custom tag to reverse a String
public class StringReverseTag extends SimpleTagSupport {
private String data;
public void setData(String data) {
this.data=data;
}
public void doTag() throws JspException, IOException {
JspWriter out = getJspContext().getOut();
StringBuffer sb = new StringBuffer(data);
sb.reverse();
out.print(sb);
}
}
Prof. AshishSingh Bhatia 11
TLD File
<tag>
<description>StringReverse Tag</description>
<name>string</name>
<tag-class>mytag.StringReverseTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>data</name>
<required>true</required>
</attribute>
</tag>
Prof. AshishSingh Bhatia 12
JSP FILE
<html>
<head>
<title>Tag Example</title>
</head>
<body>
<%@ taglib uri="/WEB-INF/tlds/tag.tld" prefix="ashish" %>
<ashish:string data="EARTH"/>
</body>
</html>
Prof. AshishSingh Bhatia 13
Including Tag body in Tag Output
 getJspBody().invoke(null)
 null means the resulting output of that JSP content is passed verbatim to the client.
 doTag() has no way to access the tag body output.
 Example
In JSP File : <ashish:tag> This is the test </ashish:tag>
In Java File
out.print(“<b>Hello World</b> <br/>”);
getJSPBody().invoke(null);
out.print(“<b>This is my tag</b>”);
In Tag File : <body-content>scriptless</body-content>
Prof. AshishSingh Bhatia 14
Using Tag Files
 Java Based Custom Tag
 What we have seen is Java Based Custom Tag
 Tag handler class is Java File
 JSP Based Custom Tag [ Tag Files ]
 Tag Handler class is JSP file
 When to use which ?
 Simple Rule : Use Java Based Custom Tag when lot of java code is involved. IF more
is of formatting use JSP Based Custom Tag.
 Remember Tag files run only on JSP 2.0.
 Java Base Tag have no such restriction.
Prof. AshishSingh Bhatia 15
JSP Based Custom Tag
 Create a JSP Base tag file
 Create a JSP page that uses the tag file
Prof. AshishSingh Bhatia 16
Structure
tagdemo
index.jsp
WEB-INF tags helloworld.tag
Prof. AshishSingh Bhatia 17
helloworld.tag and index.jsp
Tag File [ helloworld.tag ]
<b>Hello World</b>
JSP File
<html>
<head><title>Tag Example</title></head>
<body>
<%@ taglib tagdir="/WEB-INF/tags" prefix="ashish" %>
<ashish:helloworld/>
</body>
</html>
Prof. AshishSingh Bhatia 18
String Reverse [ Using Attribute and Tag File ]
 Tag File [ reverse.tag ]
<%@ attribute name="data" required="true" %>
<%
StringBuffer sb = new StringBuffer(data);
sb.reverse();
%>
<%= sb %>
 JSP File
<%@ taglib tagdir="/WEB-INF/tags" prefix="ashish" %>
<ashish:reverse data="EARTH"/>
Prof. AshishSingh Bhatia 19
Using Body part of the tag using Tag File
 Use <jsp:Body/> to get the out put of body
 Example
In JSP File : <ashish:test> This is the test </ashish:test>
In Tag File [ test.tag ] :
<b>Hello World</b> <br/>
<jsp:doBody/>
<b>This is my tag</b>
Prof. AshishSingh Bhatia 20
END OF SESSION
21Prof. AshishSingh Bhatia

More Related Content

PDF
19servlets
PPSX
CodeIgniter L5 email & user agent & security
PPTX
Jsp elements
DOCX
DOCX
บทที่5
PDF
Django Rest Framework and React and Redux, Oh My!
PPT
jQuery Tips Tricks Trivia
19servlets
CodeIgniter L5 email & user agent & security
Jsp elements
บทที่5
Django Rest Framework and React and Redux, Oh My!
jQuery Tips Tricks Trivia

Similar to JSP : Creating Custom Tag (10)

PPTX
Advance java session 15
PPT
Session 9 : intro to custom tags-classic tag - Giáo trình Bách Khoa Aptech
PPTX
Implementing jsp tag extensions
PPTX
JSP custom tags
PDF
Download full ebook of JSP Tag Libraries Gal Shachor instant download pdf
PPT
PDF
Transformation of Java Server Pages: A Modern Approach
PPTX
4. jsp
PDF
Lecture 5 JSTL, custom tags, maven
PDF
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 8 ...
Advance java session 15
Session 9 : intro to custom tags-classic tag - Giáo trình Bách Khoa Aptech
Implementing jsp tag extensions
JSP custom tags
Download full ebook of JSP Tag Libraries Gal Shachor instant download pdf
Transformation of Java Server Pages: A Modern Approach
4. jsp
Lecture 5 JSTL, custom tags, maven
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 8 ...
Ad

More from AshishSingh Bhatia (8)

PDF
Servlet Event framework
PDF
Servlet Filter
PDF
Dom Basics
PDF
Java script
PDF
Java I/O Part 2
PDF
Java I/O Part 1
PDF
Nested and Enum Type in Java
PDF
Http and Servlet basics
Servlet Event framework
Servlet Filter
Dom Basics
Java script
Java I/O Part 2
Java I/O Part 1
Nested and Enum Type in Java
Http and Servlet basics
Ad

Recently uploaded (20)

PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Presentation on HIE in infants and its manifestations
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
master seminar digital applications in india
PPTX
Lesson notes of climatology university.
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Final Presentation General Medicine 03-08-2024.pptx
Presentation on HIE in infants and its manifestations
FourierSeries-QuestionsWithAnswers(Part-A).pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Anesthesia in Laparoscopic Surgery in India
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
master seminar digital applications in india
Lesson notes of climatology university.
A systematic review of self-coping strategies used by university students to ...
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
202450812 BayCHI UCSC-SV 20250812 v17.pptx
O5-L3 Freight Transport Ops (International) V1.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Microbial diseases, their pathogenesis and prophylaxis
3rd Neelam Sanjeevareddy Memorial Lecture.pdf

JSP : Creating Custom Tag

  • 1. CUSTOM TAG LIBRARY Prof. AshishSingh Bhatia 1Prof. AshishSingh Bhatia
  • 2. Two Approach  Java Based Custom Tag  No Version Restriction  Uses Java file for Tag Handling  Preferred when lot of java code is required for getting output  JSP Based Custom Tag  Only from JSP 2.0  Uses JSP file for Tag Handling  Preferred when lot of html code is required for getting output. Prof. AshishSingh Bhatia 2
  • 3. Tag Library Components  Tag handler class that defines the tag behavior.  The TLD file that maps the XML element names to the tag implementation.  The JSP file that uses the tag library. 3Prof. AshishSingh Bhatia
  • 4. The Tag Handler Class  Class that tells what to do when system see the tag.  Class must implement SimpleTag interface.  In practice, extends SimpleTagSupport which implements SimpleTag.  javax.servlet.jsp.tagext package.  Every Tag Handler class must have 0 argument constructor.  doTag() is the main method for tag handling.  We need JspWriter [ getJspContext().getOut() ]  New instance is created for every tag occurrence on the page. Prof. AshishSingh Bhatia 4
  • 5. Directory Structure hellowordtag index.jsp WEB-INF tlds tag.tld classes mytag HelloWorldTag.java Prof. AshishSingh Bhatia 5
  • 6. Tag Handler Class package mytag; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.io.*; public class HelloWorldTag extends SimpleTagSupport { public void doTag() throws JspException, IOException { JspWriter out = getJspContext().getOut(); out.print("<b>Hello World</b>"); } } Prof. AshishSingh Bhatia 6
  • 7. TLD : Tag Library Descriptor <?xml version="1.0" ?> <taglib version="2.0"> <tlib-version>1.0</tlib-version> <short-name>mytag</short-name> <tag> <description>HelloWorld Tag</description> <name>helloworld</name> <tag-class>mytag.HelloWorldTag</tag-class> <body-content>empty</body-content> </tag> </taglib> Prof. AshishSingh Bhatia 7 Optional Required Element empty, scriptless, tagdependent, JSP
  • 8. JSP File <html> <head> <title>Tag Example</title> </head> <body> <%@ taglib uri="/WEB-INF/tlds/tag.tld" prefix="ashish" %> <ashish:helloworld/> </body> </html> Prof. AshishSingh Bhatia 8
  • 9. Assigning Attributes to Tags  For every attribute we need a set method in tag handler class  <prefix:tag attribute1=“value1” attribute2=“value2” … /> Prof. AshishSingh Bhatia 9 setAttribute1(String value) setAttribute2(String value)
  • 10. TLD File <attribute> <description>…. </description> <name> X </name> <required> true/false </required> </attribute> Prof. AshishSingh Bhatia 10 X = Must match with the variable in class
  • 11. Example : Custom tag to reverse a String public class StringReverseTag extends SimpleTagSupport { private String data; public void setData(String data) { this.data=data; } public void doTag() throws JspException, IOException { JspWriter out = getJspContext().getOut(); StringBuffer sb = new StringBuffer(data); sb.reverse(); out.print(sb); } } Prof. AshishSingh Bhatia 11
  • 13. JSP FILE <html> <head> <title>Tag Example</title> </head> <body> <%@ taglib uri="/WEB-INF/tlds/tag.tld" prefix="ashish" %> <ashish:string data="EARTH"/> </body> </html> Prof. AshishSingh Bhatia 13
  • 14. Including Tag body in Tag Output  getJspBody().invoke(null)  null means the resulting output of that JSP content is passed verbatim to the client.  doTag() has no way to access the tag body output.  Example In JSP File : <ashish:tag> This is the test </ashish:tag> In Java File out.print(“<b>Hello World</b> <br/>”); getJSPBody().invoke(null); out.print(“<b>This is my tag</b>”); In Tag File : <body-content>scriptless</body-content> Prof. AshishSingh Bhatia 14
  • 15. Using Tag Files  Java Based Custom Tag  What we have seen is Java Based Custom Tag  Tag handler class is Java File  JSP Based Custom Tag [ Tag Files ]  Tag Handler class is JSP file  When to use which ?  Simple Rule : Use Java Based Custom Tag when lot of java code is involved. IF more is of formatting use JSP Based Custom Tag.  Remember Tag files run only on JSP 2.0.  Java Base Tag have no such restriction. Prof. AshishSingh Bhatia 15
  • 16. JSP Based Custom Tag  Create a JSP Base tag file  Create a JSP page that uses the tag file Prof. AshishSingh Bhatia 16
  • 18. helloworld.tag and index.jsp Tag File [ helloworld.tag ] <b>Hello World</b> JSP File <html> <head><title>Tag Example</title></head> <body> <%@ taglib tagdir="/WEB-INF/tags" prefix="ashish" %> <ashish:helloworld/> </body> </html> Prof. AshishSingh Bhatia 18
  • 19. String Reverse [ Using Attribute and Tag File ]  Tag File [ reverse.tag ] <%@ attribute name="data" required="true" %> <% StringBuffer sb = new StringBuffer(data); sb.reverse(); %> <%= sb %>  JSP File <%@ taglib tagdir="/WEB-INF/tags" prefix="ashish" %> <ashish:reverse data="EARTH"/> Prof. AshishSingh Bhatia 19
  • 20. Using Body part of the tag using Tag File  Use <jsp:Body/> to get the out put of body  Example In JSP File : <ashish:test> This is the test </ashish:test> In Tag File [ test.tag ] : <b>Hello World</b> <br/> <jsp:doBody/> <b>This is my tag</b> Prof. AshishSingh Bhatia 20
  • 21. END OF SESSION 21Prof. AshishSingh Bhatia