A common misconception about JavaServer Pages is that they are executed by the web container. Actually they only supply the web container with the instructions it needs to generate the Java code for a specialized servlet. That generated code is then compiled by the web container, and the resulting class is executed. To illustrate this concept, consider the simple JSP listing below:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <html> <head> <title>JSP to Servlet</title> </head> <body> <jsp:useBean id="bean" class="com.webucator.bean.NameBean"/> <jsp:setProperty name="bean" property="firstName" value="Roger"/> <jsp:setProperty name="bean" property="lastName" value="Sakowski"/> My name is: <jsp:getProperty name="bean" property="fullName"/> </body> </html> |
When this page is requested, the following is displayed in a web browser:
My name is: Roger Sakowski
It seems pretty straightforward, but it’s not. The listing below is the generated source code produced by Tomcat’s web container:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | package org.apache.jsp; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; public final class MyJsp_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent { private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory(); private static java.util.List _jspx_dependants; private javax.el.ExpressionFactory _el_expressionfactory; private org.apache.AnnotationProcessor _jsp_annotationprocessor; public Object getDependants() { return _jspx_dependants; } public void _jspInit() { _el_expressionfactory = _jspxFactory.getJspApplicationContext( getServletConfig().getServletContext()). getExpressionFactory(); _jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig() .getServletContext().getAttribute( org.apache.AnnotationProcessor. class.getName()); } public void _jspDestroy() { } public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException { PageContext pageContext = null; HttpSession session = null; ServletContext application = null; ServletConfig config = null; JspWriter out = null; Object page = this; JspWriter _jspx_out = null; PageContext _jspx_page_context = null; try { response.setContentType("text/html"); 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>\r\n"); out.write("\t<head>\r\n"); out.write("\t\t<title>JSP to Servlet</title>\r\n"); out.write("\t</head>\r\n"); out.write(" \r\n"); out.write("\t<body>\r\n"); out.write("\t\t"); com.webucator.bean.NameBean bean = null; synchronized (_jspx_page_context) { bean = (com.webucator.bean.NameBean) _jspx_page_context .getAttribute("bean", PageContext.PAGE_SCOPE); if (bean == null) { bean = new com.webucator.bean.NameBean(); _jspx_page_context.setAttribute("bean", bean, PageContext.PAGE_SCOPE); } } out.write("\r\n"); out.write("\t\t"); org.apache.jasper.runtime.JspRuntimeLibrary. introspecthelper( _jspx_page_context. findAttribute("bean"), "firstName", "Roger", null, null, false); out.write("\r\n"); out.write("\t\t"); org.apache.jasper.runtime. JspRuntimeLibrary.introspecthelper( _jspx_page_context.findAttribute("bean"), "lastName", "Sakowski", null, null, false); out.write("\r\n"); out.write("\t\tMy name is: \r\n"); out.write("\t\t"); out.write(org.apache.jasper.runtime. JspRuntimeLibrary.toString((((com.webucator. bean.NameBean) _jspx_page_context. findAttribute("bean")).getFullName()))); out.write("\r\n"); out.write("\t</body>\r\n"); out.write("</html>"); } catch (Throwable t) { if (!(t instanceof 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); } } finally { _jspxFactory.releasePageContext(_jspx_page_context); } } } |
The web container compiles this code and executes the resulting servlet.
You probibly noticed that the listed code isn’t exactly the same sort of servlet you generally develop yourself, but it’s pretty easy to recognize some familiar elements. The init and destroy methods become _jspInit and _jspDestroy. The service method is _jspService.
Check out our Introduction the JavaServer Pages, Comprehensive JSP, and Advanced JSP outlines.





Being a blogger is like being in charge of your own personal insane asylum.
October 6th, 2010 at 3:00 pm