Monday, July 16, 2012

JSP Custom Tag LifeCycle (classical)



1.  Web Container loads the class (class extending TagSupport/ BodyTagSupport class).

2.  Instantiate the class with no-arg constructor

Note : above two steps happens when container sees custom tag first time in JSP page. It might be possible container will use the same object in case of re occurrence of same custom tag.

3.  Container will call the setPageContext(pageContext) method to get the reference of PageContext which will be handed over to Handler class(class extending TagSupport/BodyTagSupport)

4.  If the Tag is nested(invoked from within any other tag) call the setParent(Tag) method.

5.  If the Tag has Attribute, call attribute setter method.

6.  Call the int doStartTag() method which should return either EVAL_BODY_INCLUDE or SKIP_BODY constants.

SKIP_BODY(default) : in this case the tag handler class skips the body of tag and directly call the doEndTag().

EVAL_BODY_INCLUDE : if TLD has mentioned about <body-content> and tag have body than body of the tag will be Evaluated only once before again calling the doAfterBody() method.

7.  If doStartTag() returns EVAL_BODY_INCLUDE than int doAfterBody() will be called and which should return either SKIP_BODY or EVAL_BODY_AGAIN.

SKIP_BODY(default) : doEndTag() method will be called and body will be not evaluated again(as body is already evaluated once due to EVAL_BODY_INCLUDE).

EVAL_BODY_AGAIN : doAfterBody() method will be called again. This is the only method in custom Tag LifeCycle which can be called multiple times.

8.  Call the int doEndTag() method which should return either EVAL_PAGE or SKIP_PAGE.

EVAL_PAGE(default): the remaining JSP Page after the TAG will be evaluated.

SKIP_PAGE : the rest of the JSP page which have this TAG will skipped.   


------------------------------------------------------------------------------------------------------

Directory Structure in ECLIPSE :



-------------------------------------------------------------------------------------------------------------

 Example:

Handler class : ClassicCustomTag.java

package in.ibm;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

public class ClassicCustomTag extends TagSupport {
    int mCounter=0;
    String Movies[]= {"Dilwale","Taare Zameen Par","Hum"};

    public int doStartTag() throws JspException {
    //    mCounter=0;
        pageContext.setAttribute("movie", Movies[mCounter]);
        mCounter++;
        //return SKIP_BODY;
        return EVAL_BODY_INCLUDE;
    }
      

    public int doAfterBody() throws JspException {
        if(mCounter<Movies.length){
          
        pageContext.setAttribute("movie", Movies[mCounter]);
        mCounter++;
        //return SKIP_BODY;
        return EVAL_BODY_AGAIN;
        }else{
            return SKIP_BODY;
        }
    }
   
   
    public int doEndTag() throws JspException {
        return EVAL_PAGE;
    }
   

}


-------------------------------------------------------------------------------------------------------

JSP Page : ClassicTagTest.jsp 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@taglib uri="/WEB-INF/tlds/ClassicTag.tld" prefix="CTag" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
</head>
<body>
<table border="1">

<CTag:CT>
<tr><td>${movie}</td></tr></CTag:CT>

</table>

I am at the Bottom of the page
</body>
</html>

-----------------------------------------------------------------------------------------------------------=
 TLD File : ClassicTag.tld

<?xml version="1.0" encoding="UTF-8"?>
<taglib>
<jsp-version>1.0</jsp-version>
<tlib-version>1.2</tlib-version>
<uri>ClassicTag</uri>
<tag>
<name>CT</name>
<tag-class>in.ibm.ClassicCustomTag</tag-class>
<body-content>scriptless</body-content>
</tag>
</taglib>
----------------------------------------------------------------------------------------------------------

WEB.XML:

<?xml version="1.0" encoding="UTF-8"?>
<web-app>

  <display-name>ClassicalCustomTags</display-name>
  <welcome-file-list>
   
    <welcome-file>ClassicTagTest.jsp</welcome-file>
  </welcome-file-list>
</web-app>
 
---------------------------------------------------------------------------------------------------------

OUTPUT :