Custom Tags

JSP Tag Library:-
-->JspTag library is a library that contains set of readily available jsp tags. It is a java package holding classes, interfaces
-->These are 4 types of tag libraries
a.)Built-in jsp tag libraries(All standard action tags)
b.)JSTL
c.)3rd party jsp tag libraries
d.)Custom tag libraries(User defined)
Custom tag libraries(User defined):-
-->Writing java code in jsp is bad practice. The reasons are...
a.)Against of JSP programming principles(Tag based programming)
b.)Kills the readability of the code
c.)Kills the reusability of java code that is placed in jsp and etc...
-->To overcome these problems we need to use the following tags like html tags, jsp built-in tags, jstl tags, 3rd party tags and custom tags...
Terminologies for Custom Tags:-
a.)Tag handler class
-->The java class that defines the functionality of jsp tags is called Tag handler class
-->This class extends from TagSupport(C) directly or indirectly and defines two call back methods
  1. public int doStartTag(){•••}
  2. ->Executes when open tag encounters
  3. public int doEndTag(){•••}
  4. ->Executes when closing tag encounters
b.)TLD file (Tag Library Descriptor)
-->It is xml file where all the jsp tags of tag library are configured
-->It contains jsp tag names, tag handler class names, attributes names and etc...
c.)Empty Tag
-->Tag without body
  1. E.g: <start:example>
d.)Empty tag optional attributes
  1. E.g: <start:prime n=20>
e.)Tag with body
  1. E.g: <start:display>teja</start:display>
f.)Tag with body having optional attributes and mandatory attributes
  1. E.g: <start:display size="30" font="verdana">teja</start:display>
Steps to develop Custom TagLibrary:-
a.)Design tag library
<ABC>
<XYZ>
b.)Develop tag handler classes(src folder packages)
  1.  public class ABCTag extends TagSupport{
  2.   public int doStartTag(){
  3.       •••
  4.  }
  5.  public int doEndTag(){
  6.      •••
  7.  }
  8. }
XYZTag.java
  1. public class XYZTag extends TagSupport{
  2.   public int doStartTag(){
  3.       •••(g)
  4.  }
  5.  public int doEndTag(){
  6.      •••(g)
  7.  }
  8. }
c.)Configure JSP tags in TLD file
first.tld(xml entries)
  1. <ABC>=(f)=>ABCTag.class
  2. <XYZ>==>XYZTag.class
d.)Configure jsp tag library in web.xml file
web.xml
  1. <jsp-config>
  2.   <taglib>
  3.     <taglib-uri>demo(d)</taglib-uri>
  4.     <taglib-location>/WEB-INF/first.tld(e)</taglib-location>
  5.  </taglib>
  6. </jsp-config>
-->Every jsp tag library is identified with its taglib uri
e.)Use the jsp tags of tag library in jsp pages
test.jsp
  1. <%@taglib uri="demo" prefix="st"(c)%>
  2. ---->(b)
  3. <st:ABC/>
  4. ------->(a)
  5. <st:XYZ/>
  6. (h)-->output

Note: If two tags of two different tag libraries are used in same jsp having same name then they can be differentiated based on prefix(prefixes are user defined)

Custom Taglibrary
-->doStartTag(), doEndTag() methods are callback methods. These methods return numeric value as int constant values to provide instructions to ServletContainer towards performing additional operations on the jsp page

EVAL_PAGE(6)->Gives instruction to evaluate the remaining page
EVAL_BODY_INCLUDE(1)->Evaluatethe tag by including the body of the tag
SKIP_BODY(0)->Evaluate the tag by ignoring the body
SKIP_PAGE(5)->Stop evaluating the remaining jsp page tags
EVAL_BODY_AGAIN(2)->Evaluate the tag body for multiple times

-->pageContext object is protected member variable of TagSupport class, so in every TagHandler class the pageContext object is visible as inherited property because every TagHandler class is sub class of TagSupport class
-->Using this inherited pageContext object we can get access to other implicit objects in TagHandler class by using pageContext.getXxx()methods
pageContext.getOut(), pageContext.getServletConfig(), pageContext.getServletContext(), pageContext.getRequest(),pageContext.getResponse() and etc...
 Example App:-
CustomTaglibApp
|-->java resources
     |-->src
           |-->org.tags
                |-->ExampleTag.java
                |-->PrimeTag.java
                |-->DisplayTag.java
|-->web content
     |-->test.jsp
     |-->WEB-INF
          |-->first.tld
          |-->web.xml
Jars:- servlet-api.jar, jsp-api.jar

-->Use span tag to apply styles on one line of text, similarly use <div> tag to apply styles on multiple lines of text

Comments

Popular posts from this blog

JSP Life Cycle

Java Mail API

Project Architecture's