《JSP应用开发详解》学习:JSP的工作原理

本文详细介绍了JSP应用开发中的工作原理。当浏览器请求JSP文件时,Servlet容器首次会将其编译成Servlet类。之后的请求会直接调用已存在的Servlet,直到JSP文件修改。JSP引擎负责处理客户端请求和响应,通过HTTP协议与浏览器交互,并生成相应的HTML。以Tomcat为例,创建并访问JSP文件后,可以在服务器目录中找到编译后的Java和Class文件,其中_jspService()方法用于处理请求响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

浏览器请求服务器内的JSP文件时,服务器的Servlet容器会在第一次调用这个JSP文件时进行编译。编译完成后,该JSP实际是一个Servlet类,该Servlet产生对应的输出结果,把输出结果发送给Servlet容器,Servlet容器把这些结果以HTML文件形式发送给浏览器。

应注意,Servlet容器会在第一次调用JSP时进行编译,之后这个JSP会存在在内存中,之后再次调用无需编译(除非JSP文件有修改)。这个工作是由JSP引擎完成的。JSP引擎把客户端请求发送给JSP源文件,再把JSP源文件的相应发送给客户端。JSP引擎需要支持HTTP协议。JSP引擎在传递request和response对象之前,会通过JSP源文件创建一个类(Servlet类),并在调用时进行编译。

以下用例子说明:
在Tomcat的webapps下创建jsplearn目录,并创建helloworld.jsp文件。
helloworld.jsp内容如下:

<%@ page language="java" contentType="text/html;charset=utf-8"%>
<%@ page info="a hello wxample"%>
<html>
 <head>
  <title>理解JSP的原理</title>
 </head>
 <body>
  <center>
   <h1>
   <%
   int times = 10;
   for(int i =0;i<times;i++){
    out.println("HELLOWORLD");
    out.println("<br>");
   }
   %>
   </h1>
  </center>
 </body>
</html>

启动Tomcat后,访问路径为http://localhost:8080/jsplearn/helloworld.jsp
可以在浏览器内看到相应输出。在开发人员工具选项中,可以看到浏览器解析的HTML文件为:

<html>
 <head>
  <title>理解JSP的原理</title>
 </head>
 <body>
  <center>
   <h1>
   HELLOWORLD
   <br>
HELLOWORLD
<br>
HELLOWORLD
<br>
HELLOWORLD
<br>
HELLOWORLD
<br>
HELLOWORLD
<br>
HELLOWORLD
<br>
HELLOWORLD
<br>
HELLOWORLD
<br>
HELLOWORLD
<br>
</h1>
  </center>
 </body>
</html>

该HTML是服务器中JSP源文件经过编译被调用后,对请求的相应。
此时在Tomcat的本地目录work\Catalina\localhost\jsplearn中,可以看到JSP源文件所对应生成的helloworld_jsp.java和helloworld_jsp.class。helloworld_jsp.java就相当于一个Servlet类。

其最主要的代码片段为:

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("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("<html>\r\n");
      out.write("\t<head>\r\n");
      out.write("\t\t<title>理解JSP的原理</title>\r\n");
      out.write("\t</head>\r\n");
      out.write("\t<body>\r\n");
      out.write("\t\t<center>\r\n");
      out.write("\t\t\t<h1>\r\n");
      out.write("\t\t\t");
      int times = 10;
   for(int i =0;i<times;i++){
    out.println("HELLOWORLD");
    out.println("<br>");
   }
    out.write("\r\n");
      out.write("\t\t\t</h1>\r\n");
      out.write("\t\t</center>\r\n");
      out.write("\t</body>\r\n");
      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 {
            if (response.isCommitted()) {
              out.flush();
            } else {
              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);
    }

使用out.write来作为浏览器请求的相应。当请求该JSP源文件时,JSP引擎就会调用该_jspService()方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值