Your SlideShare is downloading. ×
JSP : Creating Custom Tag
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

JSP : Creating Custom Tag

5,709
views

Published on

Learn how to create the custom tag in JSP using SimpleTagSupport. Two approaches Java Based Custom Tag or JSP Based Custom Tag.

Learn how to create the custom tag in JSP using SimpleTagSupport. Two approaches Java Based Custom Tag or JSP Based Custom Tag.

Published in: Education, Technology, Business

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
5,709
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
118
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. CUSTOM TAG LIBRARYProf. AshishSingh Bhatia1Prof. AshishSingh Bhatia
  • 2. Two Approach Java Based Custom Tag No Version Restriction Uses Java file for Tag Handling Preferred when lot of java code isrequired for getting output JSP Based Custom Tag Only from JSP 2.0 Uses JSP file for Tag Handling Preferred when lot of html code isrequired 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 Structurehellowordtagindex.jspWEB-INFtlds tag.tldclasses mytag HelloWorldTag.javaProf. AshishSingh Bhatia 5
  • 6. Tag Handler Classpackage 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 7OptionalRequired Elementempty, 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 9setAttribute1(String value) setAttribute2(String value)
  • 10. TLD File<attribute><description>…. </description><name> X </name><required> true/false </required></attribute>Prof. AshishSingh Bhatia 10X = Must match with thevariable in class
  • 11. Example : Custom tag to reverse a Stringpublic 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
  • 12. 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
  • 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. ExampleIn JSP File : <ashish:tag> This is the test </ashish:tag>In Java Fileout.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 moreis 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 fileProf. AshishSingh Bhatia 16
  • 17. Structuretagdemoindex.jspWEB-INF tags helloworld.tagProf. AshishSingh Bhatia 17
  • 18. helloworld.tag and index.jspTag 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 ExampleIn 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 SESSION21Prof. AshishSingh Bhatia