Next page So how does a JSP actually work?

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值