JSP and JSTL Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 3.09, 2006

JSP Tag Java Interface

Part:   1  2  3 

JSP/JSTL Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Using Cookies

Using JavaBean Classes

HTTP Response Header Lines

Non ASCII Characters

JSTL and Expression Language

File Upload

Execution Context

JSP Elements

JSP Standard Tag Libraries (JSTL)

JSP Custom Tag

... Table of Contents

(Continued from previous part...)

The Servlet Class - TraceTagTest_jsp.java

As we all know that JSP pages are translated into Servlet classes before execution. So we actually review the translate Servlet class to see how the tag extension facility connects the Servlet class to the tag class.

To review the Servlet class translated from TraceTagTest.jsp, open TraceTagTest_jsp.java in \local\jakarta-tomcat-4.1.18\work\standalone\localhost\_, you will see:

package org.apache.jsp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import org.apache.jasper.runtime.*;

public class TraceTagTest_jsp extends HttpJspBase {

  private static java.util.Vector _jspx_includes;

  private org.apache.jasper.runtime.TagHandlerPool 
     _jspx_tagPool_hy_trace_myAtt;

  public TraceTagTest_jsp() {
    _jspx_tagPool_hy_trace_myAtt 
       = new org.apache.jasper.runtime.TagHandlerPool();
  }

  public java.util.List getIncludes() {
    return _jspx_includes;
  }

  public void _jspDestroy() {
    _jspx_tagPool_hy_trace_myAtt.release();
  }

  public void _jspService(HttpServletRequest request, 
     HttpServletResponse response)
        throws java.io.IOException, ServletException {

    JspFactory _jspxFactory = null;
    javax.servlet.jsp.PageContext pageContext = null;
    HttpSession session = null;
    ServletContext application = null;
    ServletConfig config = null;
    JspWriter out = null;
    Object page = this;
    JspWriter _jspx_out = null;

    try {
      _jspxFactory = JspFactory.getDefaultFactory();
      response.setContentType("text/html;charset=UTF-8");
      pageContext = _jspxFactory.getPageContext(this, request, response,
      			null, true, 8192, true);
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      session = pageContext.getSession();
      out = pageContext.getOut();
      _jspx_out = out;

      out.write("<html>");
      out.write("<body>");
      if (_jspx_meth_hy_trace_0(pageContext))
        return;
      out.write("</body>");
      out.write("</html>");
    } catch (Throwable t) {
      out = _jspx_out;
      if (out != null && out.getBufferSize() != 0)
        out.clearBuffer();
      if (pageContext != null) pageContext.handlePageException(t);
    } finally {
      if (_jspxFactory != null) 
         _jspxFactory.releasePageContext(pageContext);
    }
  }

  private boolean _jspx_meth_hy_trace_0(
     javax.servlet.jsp.PageContext pageContext)
          throws Throwable {
    JspWriter out = pageContext.getOut();
    /* ----  hy:trace ---- */
    herong.TraceTag _jspx_th_hy_trace_0 = (herong.TraceTag) 
       _jspx_tagPool_hy_trace_myAtt.get(herong.TraceTag.class);
    _jspx_th_hy_trace_0.setPageContext(pageContext);
    _jspx_th_hy_trace_0.setParent(null);
    _jspx_th_hy_trace_0.setMyAtt("my value");
    int _jspx_eval_hy_trace_0 = _jspx_th_hy_trace_0.doStartTag();
    if (_jspx_eval_hy_trace_0 != 
       javax.servlet.jsp.tagext.Tag.SKIP_BODY) {
      do {
        out.write("JSP body");
        out.write("<br/>");
        int evalDoAfterBody = _jspx_th_hy_trace_0.doAfterBody();
        if (evalDoAfterBody != 
           javax.servlet.jsp.tagext.BodyTag.EVAL_BODY_AGAIN)
          break;
      } while (true);
    }
    if (_jspx_th_hy_trace_0.doEndTag() ==
       javax.servlet.jsp.tagext.Tag.SKIP_PAGE)
      return true;
    _jspx_tagPool_hy_trace_myAtt.reuse(_jspx_th_hy_trace_0);
    return false;
  }
}

As you can see, the entire hy:trace element was translated into a method call, _jspx_meth_hy_trace_0(pageContext). In that method, an object was instantiated from my trace tag class. Then interface methods were called one by one in the same order as we discussed in the previous section. The "do" loop was there to re-evaluate the body based on the returning flag of doAfterBody() call.

After finishing this trace tag example, I had a very good understanding of how the tag interface offered by the extension facility works now. How about you?

Dummy Implementation of IterationTag Interface - TagSupport Class

J2EE supplied a dummy implementation, called TagSupport, to the IterationTag interface. If you are design a new tag class, you may extend your class from TagSupport, instead of implement InterationTag. Extending your tag class from TagSupport gives you two advantages.

1. Need to code any required method, if you are happy with the implementation of that method inside TagSupport.

2. TagSupport provides you the pageContext object ready to use. No need for you to code setPageContext() and save it in your tag object.

Examples of using TagSupport will be include in my other notes on custom tags.

Part:   1  2  3 

Dr. Herong Yang, updated in 2006
JSP and JSTL Tutorials - Herong's Tutorial Notes - JSP Tag Java Interface