SlideShare a Scribd company logo
Implementing JSP tag extensions
Agenda
• Understand and use jsp: tags
• Developing JSP pages using custom tags
Use of JSP custom tag
• Custom tag is a user defined JSP language element
• It is translated into servlet
• Tag is converted into operation on tag handler
• Container invokes those operation on the JSP page
• Three types of tag
- Simple tag
- Tags with attributes
- Tags with body content
Use of JSP custom tag
• Java-based tags
- Components of a tag library
- Basic tags
- Tags that use attributes
- Tags that use body content
- Tags that optionally use body content
• JSP-based tags (tag files)
- Components of a tag library
- Basic tags
- Tags that use attributes
- Tags that use body content
Components that make up a tag library
• The Tag Handler Class
- Java code that says what to output
- Must implement javax.servlet.jsp.tagext.SimpleTag
• Usually extends SimpleTagSupport
• Goes in same directories as servlet class files and bean
• The Tag Library Descriptor File
• XML file describing tag name, attributes, and implementing tag handler class
- Goes under WEB-IN
• The JSP File
- Imports a tag library (referencing URL of descriptor file)
- Defines tag prefix
- Uses tags
Simple illustration
• Create a java class that acts as a tag handler
package mytag;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;
public class HelloTag extends SimpleTagSupport
{
public void doTag() throws JspException, IOException {
JspWriter out = getJspContext().getOut(); out.println("Hello Custom Tag!");
} }
Simple illustration
• compile above class and copy it in a directory available in environment
variable CLASSPATH. Finally create following tag library file:
<Tomcat-Installation-Directory>webappsROOTWEB-INFcustom.tld.
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<short-name>Example TLD</short-name>
<tag> <name>Hello</name>
<tag-class>mytag.HelloTag</tag-class>
<body-content>empty</body-content> </tag>
</taglib>
Simple illustration
• Now it's time to use above defined custom tag Hello in our JSP
program as follows
<%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%>
<html> <head>
<title>A sample custom tag</title>
</head>
<body>
<ex:Hello/>
</body>
</html>
Accessing tag with body content
• Consider you want to define a custom tag named <ex:Hello> and you
want to use it in the following fashion with a body:
<ex:Hello>
This is message body
</ex:Hello>
Tag file
package mytag;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;
public class HelloTag extends SimpleTagSupport {
StringWriter sw = new StringWriter();
public void doTag() throws JspException, IOException {
getJspBody().invoke(sw);
getJspContext().getOut().println(sw.toString());
}
}
TLD
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<short-name>Example TLD with Body</short-name>
<tag> <name>Hello</name>
<tag-class>com.tutorialspoint.HelloTag</tag-class>
<body-content>scriptless</body-content>
</tag>
</taglib>
JSP
<%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%>
<html>
<head>
<title>A sample custom tag</title>
</head>
<body>
<ex:Hello> This is message body </ex:Hello>
</body>
</html>
Elements in TLD
Summary
• For each custom (Java-based) tag, you need
- A tag handler class (usually extending SimpleTagSupport
- An entry in a Tag Library Descriptor file
- A JSP file that imports it, specifies prefix, and uses it
• Simple tags
- Generate output in doTag, body-content is empty
• Attributes
- Define setAttributeName method.
- Add attribute entries to TLD file.
• Body content
- body-content is scriptless
- doTag calls getJspBody().invoke(null)

More Related Content

PPTX
Advance java session 20
PPTX
Advance java session 2
PDF
Ch. 13 filters and wrappers
PDF
4η διάλεξη Τεχνολογίες Παγκόσμιου Ιστού
PDF
Basic terminologies for a developer
PDF
Ch5 beeing an application
PDF
Servlet and JSP
PDF
Shootout! template engines on the jvm
Advance java session 20
Advance java session 2
Ch. 13 filters and wrappers
4η διάλεξη Τεχνολογίες Παγκόσμιου Ιστού
Basic terminologies for a developer
Ch5 beeing an application
Servlet and JSP
Shootout! template engines on the jvm

What's hot (20)

PPT
MuleSoft ESB Object Store
PPTX
Advance java session 9
PPTX
MuleSoft ESB Payload Encrypt using Anypoint Security
PPTX
MuleSoft ESB Scripting Example
PDF
Introduction to AJAX
PPTX
MuleSoft ESB Filtering data instead of Looping
PDF
MuleSoft ESB Shared Library
PPTX
Road Trip To Component
PDF
Ch. x web performance
PDF
Mini-Training: NancyFX
PDF
What's new in MySQL 5.5? FOSDEM 2011
ODP
Servlets
PPTX
AngularJS
PDF
Grails 4 and Micronaut at Devnexus 2019
PDF
Java Server Pages
PDF
Introduction to Node.js Platform
PDF
Spring I/O 2012: Natural Templating in Spring MVC with Thymeleaf
PPT
Getting started into mySQL
PPTX
Node.js
PPTX
No Container: a Modern Java Stack with Bootique
MuleSoft ESB Object Store
Advance java session 9
MuleSoft ESB Payload Encrypt using Anypoint Security
MuleSoft ESB Scripting Example
Introduction to AJAX
MuleSoft ESB Filtering data instead of Looping
MuleSoft ESB Shared Library
Road Trip To Component
Ch. x web performance
Mini-Training: NancyFX
What's new in MySQL 5.5? FOSDEM 2011
Servlets
AngularJS
Grails 4 and Micronaut at Devnexus 2019
Java Server Pages
Introduction to Node.js Platform
Spring I/O 2012: Natural Templating in Spring MVC with Thymeleaf
Getting started into mySQL
Node.js
No Container: a Modern Java Stack with Bootique
Ad

Similar to Advance java session 15 (20)

PDF
JSP : Creating Custom Tag
PPTX
JSP custom tags
PPT
Session 9 : intro to custom tags-classic tag - Giáo trình Bách Khoa Aptech
PPTX
Implementing jsp tag extensions
PPTX
SCWCD : Java server pages CHAP : 9
PPTX
Web programming-Introduction to JSP.pptx
PDF
PPTX
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
PPTX
Wt unit 4
PPTX
Advance java session 16
PPTX
Jsp and jstl
PPTX
java server pages directives and processing
PPTX
PPTX
Java Server Pages
PPTX
Cis 274 intro
PDF
005432796.pdf
PDF
Lecture 5 JSTL, custom tags, maven
PPTX
JSP.pptx
PDF
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...
PPTX
Pi j4.1 packages
JSP : Creating Custom Tag
JSP custom tags
Session 9 : intro to custom tags-classic tag - Giáo trình Bách Khoa Aptech
Implementing jsp tag extensions
SCWCD : Java server pages CHAP : 9
Web programming-Introduction to JSP.pptx
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
Wt unit 4
Advance java session 16
Jsp and jstl
java server pages directives and processing
Java Server Pages
Cis 274 intro
005432796.pdf
Lecture 5 JSTL, custom tags, maven
JSP.pptx
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...
Pi j4.1 packages
Ad

More from Smita B Kumar (16)

PPTX
Advance java session 19
PPTX
Advance java session 18
PPTX
Advance java session 17
PPTX
Advance java session 14
PPTX
Advance java session 13
PPTX
Advance java session 12
PPTX
Advance java session 11
PPTX
Advance java session 10
PPTX
Advance java session 8
PPTX
Advance java session 7
PPTX
Advance java session 6
PPTX
Advance java session 5
PPTX
Advance java session 4
PPTX
Advance java session 3
PPTX
JEE session 1
PPTX
01 introduction to struts2
Advance java session 19
Advance java session 18
Advance java session 17
Advance java session 14
Advance java session 13
Advance java session 12
Advance java session 11
Advance java session 10
Advance java session 8
Advance java session 7
Advance java session 6
Advance java session 5
Advance java session 4
Advance java session 3
JEE session 1
01 introduction to struts2

Recently uploaded (20)

PPTX
Big Data Technologies - Introduction.pptx
PDF
cuic standard and advanced reporting.pdf
PPTX
Cloud computing and distributed systems.
PDF
Machine learning based COVID-19 study performance prediction
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Encapsulation theory and applications.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Empathic Computing: Creating Shared Understanding
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
A Presentation on Artificial Intelligence
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
KodekX | Application Modernization Development
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Unlocking AI with Model Context Protocol (MCP)
Big Data Technologies - Introduction.pptx
cuic standard and advanced reporting.pdf
Cloud computing and distributed systems.
Machine learning based COVID-19 study performance prediction
Spectral efficient network and resource selection model in 5G networks
Dropbox Q2 2025 Financial Results & Investor Presentation
Encapsulation theory and applications.pdf
Review of recent advances in non-invasive hemoglobin estimation
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Empathic Computing: Creating Shared Understanding
Advanced methodologies resolving dimensionality complications for autism neur...
A Presentation on Artificial Intelligence
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Understanding_Digital_Forensics_Presentation.pptx
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
NewMind AI Weekly Chronicles - August'25 Week I
Mobile App Security Testing_ A Comprehensive Guide.pdf
KodekX | Application Modernization Development
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Unlocking AI with Model Context Protocol (MCP)

Advance java session 15

  • 1. Implementing JSP tag extensions
  • 2. Agenda • Understand and use jsp: tags • Developing JSP pages using custom tags
  • 3. Use of JSP custom tag • Custom tag is a user defined JSP language element • It is translated into servlet • Tag is converted into operation on tag handler • Container invokes those operation on the JSP page • Three types of tag - Simple tag - Tags with attributes - Tags with body content
  • 4. Use of JSP custom tag • Java-based tags - Components of a tag library - Basic tags - Tags that use attributes - Tags that use body content - Tags that optionally use body content • JSP-based tags (tag files) - Components of a tag library - Basic tags - Tags that use attributes - Tags that use body content
  • 5. Components that make up a tag library • The Tag Handler Class - Java code that says what to output - Must implement javax.servlet.jsp.tagext.SimpleTag • Usually extends SimpleTagSupport • Goes in same directories as servlet class files and bean • The Tag Library Descriptor File • XML file describing tag name, attributes, and implementing tag handler class - Goes under WEB-IN • The JSP File - Imports a tag library (referencing URL of descriptor file) - Defines tag prefix - Uses tags
  • 6. Simple illustration • Create a java class that acts as a tag handler package mytag; import javax.servlet.jsp.tagext.*; import javax.servlet.jsp.*; import java.io.*; public class HelloTag extends SimpleTagSupport { public void doTag() throws JspException, IOException { JspWriter out = getJspContext().getOut(); out.println("Hello Custom Tag!"); } }
  • 7. Simple illustration • compile above class and copy it in a directory available in environment variable CLASSPATH. Finally create following tag library file: <Tomcat-Installation-Directory>webappsROOTWEB-INFcustom.tld. <taglib> <tlib-version>1.0</tlib-version> <jsp-version>2.0</jsp-version> <short-name>Example TLD</short-name> <tag> <name>Hello</name> <tag-class>mytag.HelloTag</tag-class> <body-content>empty</body-content> </tag> </taglib>
  • 8. Simple illustration • Now it's time to use above defined custom tag Hello in our JSP program as follows <%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%> <html> <head> <title>A sample custom tag</title> </head> <body> <ex:Hello/> </body> </html>
  • 9. Accessing tag with body content • Consider you want to define a custom tag named <ex:Hello> and you want to use it in the following fashion with a body: <ex:Hello> This is message body </ex:Hello>
  • 10. Tag file package mytag; import javax.servlet.jsp.tagext.*; import javax.servlet.jsp.*; import java.io.*; public class HelloTag extends SimpleTagSupport { StringWriter sw = new StringWriter(); public void doTag() throws JspException, IOException { getJspBody().invoke(sw); getJspContext().getOut().println(sw.toString()); } }
  • 11. TLD <taglib> <tlib-version>1.0</tlib-version> <jsp-version>2.0</jsp-version> <short-name>Example TLD with Body</short-name> <tag> <name>Hello</name> <tag-class>com.tutorialspoint.HelloTag</tag-class> <body-content>scriptless</body-content> </tag> </taglib>
  • 12. JSP <%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%> <html> <head> <title>A sample custom tag</title> </head> <body> <ex:Hello> This is message body </ex:Hello> </body> </html>
  • 14. Summary • For each custom (Java-based) tag, you need - A tag handler class (usually extending SimpleTagSupport - An entry in a Tag Library Descriptor file - A JSP file that imports it, specifies prefix, and uses it • Simple tags - Generate output in doTag, body-content is empty • Attributes - Define setAttributeName method. - Add attribute entries to TLD file. • Body content - body-content is scriptless - doTag calls getJspBody().invoke(null)