JSP Tag Libraries
Lec - 38
Last Lecture Example
We incorporated JavaBeans in “CourseOutline” Example
But still have to write java code insidejava.jsp & web.jsp
As discussed, JSPs are built forpresentation purpose
Last Lecture Example
What if we replace all code of scriptlet with onesingle line (custom tag)
   <%
        CourseDAO courseDAO = new CourseDAO();
          ……
         for ( ……. ) {
          ……..
         }
         ……
    %>
<mytag:displaycourse pageName=“java” />
Contents
What is a Custom Tag ?
Why build Custom Tag?
Types of Tags
Developing Custom Tags
Using Custom Tags
Examples
What is Custom Tag ?
A user defined component to performcertain action.
Provides mechanism for encapsulatingcomplex functionality for use in JSPs.
We have already seen & used many built-in tags
<jsp:useBean/>
<jsp:include/>
<jsp:forward/> etc
Advantages
More cleaner separation of processinglogic and presentation, than Java Beans
Have access to all the JSP page objects
Can be customized using attributes
Types of Tags
Types of Tags
1.Simple Tag
2.Tag with Attributes
3.Tag with Body
Types of tags
1.Simple Tags
Start and End of tag
No body within tag
No Attributes
For Example: < mytag:hello />
Tag libraryprefix
Tag Name
Types of tags
2.Tags with attributes
Start and End of tag
Attributes within tag
No body enclosed
For Example :
< mytag:hello attribute=“value” />
Types of tags
3.Tags with body
Start and End of tag
Body enclosed within tag
Attributes (optional)
For Example :
 
<mytag:hello optional_attributes …..>
          some body
      </mytag:hello>
Building Custom Tags
Building Custom Tags
We can build custom tag using either ofthe following specs
JSP 1.2
JSP 2.0
We will use JSP 2.0
Building Custom Tags - Steps
1.Develop the Tag Handler class
2.Write the Tag Library Descriptor (tld) file
3.Deployment
1 - Tag Handler class
Tag Handler is the java class
Implicitly called when the associated tag isencountered in the JSP
Must implement SimpleTag interface
Usually extend SimpleTagSupport class. For example
    public class MyTagHandler
extends SimpleTagSupport {
      …………..
    }
1 - Tag Handler class cont.
doTag() method
By default does nothing
Need to implement / override to codetag’s functionality
Invoked when the end element of thetag encountered
1 - Tag Handler class cont.
Implicit objects are available to tag handlerclass through pageContext object
pageContext object can be obtained usinggetJspContext() method.
For example, to retrieve out object
PageContext pc = (PageContext)getJspContext();
    JspWriter out = pc.getOut();
2 - Tag Library Descriptor (tld)
XML based document
Specifies information required by the JSPcontainer such as:
Tag library version
JSP version
Tag name
Tag handler class name
Attribute names etc.
3 - Deployment
Place Tag Handler class in myapp/WEB-INF/classes folder of web application
Place TLD file in myapp/WEB-INF/tldsfolder
Using Custom Tags
Using Custom Tags
Use “taglib” directive in JSP to refer to the tag library
   <%@ taglib uri = “TLDFileName” prefix = “mytag” %>
Call the tag by its name as defined in TLD
If tag name defined in TLD is hello then we write
mytag:hello />
Behind the Scenes
Container calls the appropriate Tag Handler
Tag Handler will write the appropriate response back to thepage
Building Simple Tag
Building Simple Tag
A simple tag that displays “Hello World”
Approach
Extend Tag Handler class fromSimpleTagSupport class
Override doTag() method
Build TLD file
Deploy
Example Code
Building Simple Tag
(displays “Hello World” )
Building Tag with Attributes
Building Tags with Attributes
 E.g. <mytag:hello attribute=“value” />
To handle attributes, need to add
Instance variables and
Corresponding setter methods
Container call these setter methods
Passed the custom tag attribute’s value asarguments
Building Tag with Attribute (cont.)
Example:
We will modify our Course Outline Example toincorporate tags
Based on attribute value, tag will display therespective course outline
Example Code
Building Tag with Attribute
(Modifying CourseOutline Example)