Next page So how does a JSP actually work?

来源:互联网 发布:淘宝手机找回是真的吗 编辑:程序博客网 时间:2024/06/07 02:39

So how does a JSP actually work?

It is turned into a special kind of servlet. The code is generated by a JSP compiler, which generates Java code out of it. This is the Java code that will be run while you test your JSPs. Sometimes, you’ll need to take a look a the code to understand what his happening

Tomcat, for example, places the intermediate .java files in “tomcat_root"/work/Catalina/localhost/"webapp name“.

JBoss places these in “jboss home"/server/default/work/jboss.web/localhost/"webapp name

Tomcat retains the .java source by default. This will vary by servlet container implementation, some require you to turn a debug option on to save source

Tomcat, by default, places the JSP code in the org.apache.jsp package.

Here is the code that is generated by Tomcat for the example above:

import javax.servlet.jsp.*;public final class hello_jsp extends org.apache.jasper.runtime.HttpJspBase    implements org.apache.jasper.runtime.JspSourceDependent {  private static java.util.Vector _jspx_dependants;  public java.util.List getDependants() {    return _jspx_dependants;  }  public void _jspService(HttpServletRequest request, HttpServletResponse response)        throws java.io.IOException, ServletException {    JspFactory _jspxFactory = null;    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");      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("\n");      out.write("<html>\n    ");      out.write("<head>\n      ");      out.write("<title>Hello JSP");      out.write("</title>\n    ");      out.write("</head>\n    ");      out.write("<body>\n      ");      out.write("<h1>Hello World");      out.write("</h1>\n      The time is ");      out.write(String.valueOf( new java.util.Date() ));      out.write("\n    ");      out.write("</body>\n");      out.write("</html>\n");    } catch (Throwable t) {      if (!(t instanceof javax.servlet.jsp.SkipPageException)){        out = _jspx_out;        if (out != null && out.getBufferSize() != 0)          out.clearBuffer();        if (pageContext != null) pageContext.handlePageException(t);      }    } finally {      if (_jspxFactory != null) _jspxFactory.releasePageContext(pageContext);    }  }}

Based on what you already know about Servlets, what do you think of the time writing this as a JSP has saved you? We’ll look at some more detail on other convenient things done for you by the JSP compiler later. If you study this code above, you can probably figure most of them out without the specification!

0 0