1. Hello.jsp文件内容如下:

Hello!当前韶光:${currentTime}

2. servlet代码

下面代码通过表明来处理/hello的要求, 并在代码中将要求转发到上述hello.jsp.

@WebServletpublic class HelloServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { DateFormat dateFormat = new SimpleDateFormat; String currentTime = dateFormat.format(new Date); req.setAttribute(,currentTime); req.getRequestDispatcher.forward(req,resp); }}

jsp图形图解JSP运行道理和进程 Angular

3. 运行做事器并访问

这时用everything搜索本机上的hello_jsp.java文件, 可以找到如下内容的文件:

package org.apache.jsp.WEB_002dINF.jsppublic final class hello_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent, org.apache.jasper.runtime.JspSourceImports { ... // 这里是最紧张的方法, 我们在jsp文件里的内容, 都在这里通过out.write写入到输出中. public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException { try { response.setContentType 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 out.write out.write out.write out.write out.write out.write out.write out.write out.write(当前韶光:\"大众) out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(, java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null)) out.write(\n\"大众) out.write out.write out.write } catch (java.lang.Throwable t) { ... } finally { _jspxFactory.releasePageContext(_jspx_page_context) } }}

这里可以看出, 当我们访问须要jsp文件时, tomcat的Jasper组件会将jsp文件翻译成java文件, 然后再编译. 连续用everything搜索hello_jsp, 可以创造还有一个文件叫hello_jsp.class, 侧面印证了我们的论断.

图形阐明我们先用图形大概阐明一下上述流程:

简洁易懂. 接下来我们在思考每一步的详细实现, 看下图:

1.客户端要求jsp文件, web做事器(tomcat等)根据jsp文件天生java文件.

2.java文件天生对应的class字节码文件,字节码文件是可以通过classloader加载进虚拟机的.

3.web容器加载class字节码文件.

4.web容器通过反射等手段建立hello_jsp实例.

5.调用对应的jspInit来进行实例初始化.

6.调用_jspservice, 响运用户要求.

7.调用jspDestroy销毁jsp_hello实例.