JSP Tutorials - Herong's Tutorial Examples - v5.11, by Herong Yang
Servlet Class Converted from JSP - TraceTagTest_jsp.java
This section describes the Servlet class converted from my test JSP page, TraceTagTest_jsp.java, which shows you how the custom tag, hy:trace, is converted.
As we all know that JSP pages are converted into Servlet classes before execution. So we can actually review the converted Servlet class to see how the tag extension facility connects the Servlet class to the tag class.
To review the Servlet class converted from TraceTagTest.jspx, open TraceTagTest_jspx.java in \local\tomcat\work\Catalina\localhost\ROOT\org\apache\jsp, you will see:
/* * Generated by the Jasper component of Apache Tomcat * Version: Apache Tomcat/9.0.12 */ package org.apache.jsp; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; public final class TraceTagTest_jspx extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent { private static final javax.servlet.jsp.JspFactory _jspxFactory = javax.servlet.jsp.JspFactory.getDefaultFactory(); private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants; static { _jspx_dependants = new java.util.HashMap<java.lang.String,java.lang.Long>(1); _jspx_dependants.put( "/WEB-INF/tlds/HyTaglib.tld", Long.valueOf(1352927783540L)); } private org.apache.jasper.runtime.TagHandlerPool _005fjspx_005ftagPool_005fhy_005ftrace_0026_005fmyAtt; private javax.el.ExpressionFactory _el_expressionfactory; private org.apache.tomcat.InstanceManager _jsp_instancemanager; public java.util.Map<java.lang.String,java.lang.Long> getDependants() { return _jspx_dependants; } public void _jspInit() { _005fjspx_005ftagPool_005fhy_005ftrace_0026_005fmyAtt = org.apache.jasper.runtime.TagHandlerPool.getTagHandlerPool( getServletConfig()); _el_expressionfactory = _jspxFactory.getJspApplicationContext( getServletConfig().getServletContext()).getExpressionFactory(); _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory .getInstanceManager(getServletConfig()); } public void _jspDestroy() { _005fjspx_005ftagPool_005fhy_005ftrace_0026_005fmyAtt.release(); } public void _jspService( final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException { final javax.servlet.jsp.PageContext pageContext; javax.servlet.http.HttpSession session = null; final javax.servlet.ServletContext application; final javax.servlet.ServletConfig config; javax.servlet.jsp.JspWriter out = null; final java.lang.Object page = this; javax.servlet.jsp.JspWriter _jspx_out = null; javax.servlet.jsp.PageContext _jspx_page_context = null; try { response.setContentType("text/html;charset=UTF-8"); pageContext = _jspxFactory.getPageContext( this, request, response, null, true, 8192, true); _jspx_page_context = pageContext; 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_005ftrace_005f0(_jspx_page_context)) return; out.write("</body>"); out.write("</html>"); } catch (java.lang.Throwable t) { if (!(t instanceof javax.servlet.jsp.SkipPageException)){ out = _jspx_out; if (out != null && out.getBufferSize() != 0) try { out.clearBuffer(); } catch (java.io.IOException e) {} if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); else throw new ServletException(t); } } finally { _jspxFactory.releasePageContext(_jspx_page_context); } } private boolean _jspx_meth_hy_005ftrace_005f0( javax.servlet.jsp.PageContext _jspx_page_context) throws java.lang.Throwable { javax.servlet.jsp.PageContext pageContext = _jspx_page_context; javax.servlet.jsp.JspWriter out = _jspx_page_context.getOut(); // hy:trace herong.TraceTag _jspx_th_hy_005ftrace_005f0 = (herong.TraceTag) _005fjspx_005ftagPool_005fhy_005ftrace_0026_005fmyAtt.get( herong.TraceTag.class); _jspx_th_hy_005ftrace_005f0.setPageContext(_jspx_page_context); _jspx_th_hy_005ftrace_005f0.setParent(null); // /TraceTagTest.jspx(10,28) name = myAtt type = java.lang.String reqTime = false required = false fragment = false deferredValue = false expectedTypeName = null deferredMethod = false methodSignature = null _jspx_th_hy_005ftrace_005f0.setMyAtt("my value"); int _jspx_eval_hy_005ftrace_005f0 = _jspx_th_hy_005ftrace_005f0.doStartTag(); if (_jspx_eval_hy_005ftrace_005f0 != javax.servlet.jsp.tagext.Tag.SKIP_BODY) { do { out.write("JSP body"); out.write("<br/>"); int evalDoAfterBody = _jspx_th_hy_005ftrace_005f0.doAfterBody(); if (evalDoAfterBody != javax.servlet.jsp.tagext.BodyTag.EVAL_BODY_AGAIN) break; } while (true); } if (_jspx_th_hy_005ftrace_005f0.doEndTag() == javax.servlet.jsp.tagext.Tag.SKIP_PAGE) { _005fjspx_005ftagPool_005fhy_005ftrace_0026_005fmyAtt.reuse( _jspx_th_hy_005ftrace_005f0); return true; } _005fjspx_005ftagPool_005fhy_005ftrace_0026_005fmyAtt.reuse( _jspx_th_hy_005ftrace_005f0); return false; } }
As you can see, the entire hy:trace element was translated into a method call, _jspx_meth_hy_005ftrace_005f0(_jspx_page_context). 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?
Table of Contents
JSP (JavaServer Pages) Overview
Tomcat Installation on Windows Systems
Syntax of JSP Pages and JSP Documents
JavaBean Objects and "useBean" Action Elements
Managing HTTP Response Header Lines
Non-ASCII Characters Support in JSP Pages
Overview of JSTL (JSP Standard Tag Libraries)
javax.servlet.jsp.tagext.* Package
javax.servlet.jsp.tagext.IterationTag Interface
IterationTag Interface Test Class - TraceTag.java
IterationTag Interface Test JSP - TraceTagTest.jspx
►Servlet Class Converted from JSP - TraceTagTest_jsp.java
TagSupport Class - Dummy Implementation of IterationTag
Multiple Tags Working Together
Using Tomcat on CentOS Systems
Connecting to SQL Server from Servlet