网站首页 > 基础教程 正文
JSP运行过程
- WEB容器JSP页面的访问请求时,它将把该访问请求交给JSP引擎去处理。Tomcat中的JSP引擎就是一个Servlet程序,它负责解释和执行JSP页面。
- 每个JSP页面在第一次被访问时,JSP引擎先将它翻译成一个Servlet源程序,接着再把这个Servlet源程序编译成Servlet的class类文件,然后再由WEB容器像调用普通Servlet程序一样的方式来装载和解释执行这个由JSP页面翻译成的Servlet程序。
实例解释
我们用一个实例来说明上面的JSP运行过程:
1. Hello.jsp文件内容如下:
Hello!
当前时间:${currentTime}
2. servlet代码
下面代码通过注解来处理/hello的请求, 并在代码中将请求转发到上述hello.jsp.
@WebServlet
public 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);
}
}
3. 运行服务器并访问
这时用everything搜索本机上的hello_jsp.java文件, 可以找到如下内容的文件:
package org.apache.jsp.WEB_002dINF.jsp
public 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实例.
猜你喜欢
- 2025-04-26 山东省管社会团体和社会服务机构年报工作启动
- 2025-04-26 别再写jsp了,Thymeleaf它不香吗?
- 2025-04-26 山一医高考录取查询全面开通
- 2025-04-26 深圳尚学堂:JSP 九大内置对象
- 2025-04-26 servlet和jsp的区别
- 2025-04-26 通告|2022年山东省基层法律服务工作者考试开始报名啦!
- 2025-04-26 一文弄懂Jasper引擎编译JSP文件的分析说明
- 2025-04-26 还在用JSP中的脚本程序吗?去掉吧,我教你快速掌握EL及JSTL
- 2025-04-26 Java杂谈(八)--Servlet/Jsp
- 2025-04-26 JSP三个指令及9个内置对象
- 最近发表
- 标签列表
-
- jsp (69)
- gitpush (78)
- gitreset (66)
- python字典 (67)
- dockercp (63)
- gitclone命令 (63)
- dockersave (62)
- linux命令大全 (65)
- pythonif (86)
- location.href (69)
- dockerexec (65)
- tail-f (79)
- queryselectorall (63)
- location.search (79)
- bootstrap教程 (74)
- deletesql (62)
- linuxgzip (68)
- 字符串连接 (73)
- html标签 (69)
- c++初始化列表 (64)
- mysqlinnodbmyisam区别 (63)
- arraylistadd (66)
- mysqldatesub函数 (63)
- window10java环境变量设置 (66)
- c++虚函数和纯虚函数的区别 (66)