JSP 编译和运行过程与JSP转移源码简单分析
Web容器处理JSP文件请求的执行过程主要包括以下4个部分:
1. 客户端发出Request请求
2. JSP Container 将JSP转译成Servlet的源代码
3. 将产生的Servlet源代码经过编译后,并加载到内存执行
4. 把结果Response(响应)至客户端
很多人都会认为JSP的执行性能会和Servlet相差很多,其实执行性能上的差别只在第一次的执行。因为JSP在执行第一次后,会被编译成Servlet的类文件,即.class,当再重复调用执行时,就直接执行第一次所产生的Servlet,而不再重新把JSP编译成Servelt。因此,除了第一次的编译会花较久的时间之外,之后JSP和Servlet的执行速度就几乎相同了。
在执行JSP网页时,通常可以分为两个时期:转译时期(Translation Time)和请求时期(Request Time)
转译时期:JSP网页转移成Servlet类。
请求时期:Servlet类执行后,响应结果至客户端。
转译期间做了两件事情:
转译时期:将JSP网页转移为Servlet源代码 .java.
编译时期:将Servlet 源代码 .java编译成 Servlet类 .class.
当JSP网页在执行时,JSP Container 会做检查工作,如果发现JSP网页有更新修改时,JSP Container 才会再次编译JSP成 Servlet; 如果JSP没有更新时,就直接执行前面所产生的Servlet.
showdate.jsp
- <%@ page language="java" contentType="text/html;charset=gb2312" import="java.text.*,java.util.*;"%>
- <html>
- <head>
- <title>Show time</title>
- </head>
- <body>
- Hello :
- <%
-
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
-
String str = format.format(new Date());
- %>
- <%=str %>
- </body>
- </html>
<%@ page language="java" contentType="text/html;charset=gb2312" import="java.text.*,java.util.*;"%>
<html>
<head>
<title>Show time</title>
</head>
<body>
Hello :
<%
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
String str = format.format(new Date());
%>
<%=str %>
</body>
</html>
当部署好 showdate.jsp 之后,启动 Tomcat服务器 。
1. 在IE浏览器中输入配置好的路径 .... showdate.jsp 请求这个页面
2. JSP Container 即Tomcat 服务器会将 showdate.jsp 转译成 showdate_jsp.java 源文件
3. 同时将 showdate_jsp.java 源文件编译成 showdate_jsp.class
4. 编译执行showdate_jsp.class 类,处理请求,返回响应,容器将生成的页面返回给客户端显示
转移成的 java 源文件 showdate_jsp.java
- package org.apache.jsp.ch04;
-
-
import javax.servlet.*;
-
import javax.servlet.http.*;
-
import javax.servlet.jsp.*;
-
import java.text.*;
-
import java.util.*;;
-
-
public final class showdate_jsp extends org.apache.jasper.runtime.HttpJspBase
-
implements org.apache.jasper.runtime.JspSourceDependent {
-
-
private static java.util.List _jspx_dependants;
-
-
public Object 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;
-
PageContext _jspx_page_context = null;
-
-
try {
- _jspxFactory = JspFactory.getDefaultFactory();
-
response.setContentType("text/html;charset=gb2312");
-
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("<html>\r\n");
-
out.write("<head>\r\n");
-
out.write("<title>Show time</title>\r\n");
-
out.write("</head>\r\n");
-
out.write("<body> \r\n");
-
out.write("\tHello : \r\n");
-
out.write("\t");
-
-
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
-
String str = format.format(new Date());
-
-
out.write("\r\n");
-
out.write("\t ");
- out.print(str );
-
out.write("\r\n");
-
out.write("</body>\r\n");
-
out.write("</html>");
-
} catch (Throwable t) {
-
-
if (!(t instanceof SkipPageException)){
- out = _jspx_out;
-
if (out != null && out.getBufferSize() != 0)
- out.clearBuffer();
-
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
- }
-
-
} finally {
-
-
if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
- }
- }
- }
package org.apache.jsp.ch04;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.text.*;
import java.util.*;;
public final class showdate_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {
private static java.util.List _jspx_dependants;
public Object 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;
PageContext _jspx_page_context = null;
try {
_jspxFactory = JspFactory.getDefaultFactory();
response.setContentType("text/html;charset=gb2312");
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("<html>\r\n");
out.write("<head>\r\n");
out.write("<title>Show time</title>\r\n");
out.write("</head>\r\n");
out.write("<body> \r\n");
out.write("\tHello : \r\n");
out.write("\t");
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
String str = format.format(new Date());
out.write("\r\n");
out.write("\t ");
out.print(str );
out.write("\r\n");
out.write("</body>\r\n");
out.write("</html>");
} catch (Throwable t) {
if (!(t instanceof SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
out.clearBuffer();
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
}
} finally {
if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
}
}
}
当 JSP 页面被转译成Servlet时,内容主要包含三个部分:
- public void _jspInit(){ ..} -- 当JSP网页一开始执行时,最先执行此方法,执行初始化工作
-
-
public void _jspDestory(){...} – JSP网页最后执行的方法
-
-
public void _jspService(HttpServletRequest request, HttpServletResponse response)
-
throws java.io.IOException, ServletException {
public void _jspInit(){ ..} -- 当JSP网页一开始执行时,最先执行此方法,执行初始化工作
public void _jspDestory(){...} – JSP网页最后执行的方法
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException {
-- JSP网页中最主要的程序都是在此执行
将 showdate.jsp 和 showdate_jsp.java 做一个简单对比
第一部分:页面属性的对比
- <%@ page language="java" contentType="text/html;charset=gb2312" %>
-
response.setContentType("text/html;charset=gb2312");
<%@ page language="java" contentType="text/html;charset=gb2312" %>
response.setContentType("text/html;charset=gb2312");
//通过 response响应设置返回客户端的页面属性
第二部分:HTML标签
- <html>
- <head>
- <title>Show time</title>
- </head>
- ..
- </html>
<html>
<head>
<title>Show time</title>
</head>
..
</html>
- out.write("\r\n");
-
out.write("<html>\r\n");
-
out.write("<head>\r\n");
-
out.write("<title>Show time</title>\r\n");
-
out.write("</head>\r\n");
-
out.write("<body> \r\n");
-
out.write("\tHello : \r\n");
-
out.write("\t");
out.write("\r\n");
out.write("<html>\r\n");
out.write("<head>\r\n");
out.write("<title>Show time</title>\r\n");
out.write("</head>\r\n");
out.write("<body> \r\n");
out.write("\tHello : \r\n");
out.write("\t");
//通过 out对象 向客户端写HTML标签
第三部分:声明的对象
- <%
-
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
-
String str = format.format(new Date());
- %>
<%
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
String str = format.format(new Date());
%>
在 _jspService 方法中声明的局部变量
- SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
-
String str = format.format(new Date());
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
String str = format.format(new Date());
第四部分:表达式
- <%=str %>
-
-
out.print(str );
<%=str %>
out.print(str ); //写 即打印str变量的值
分享到:
相关推荐
JSP文件转译而成的Servlet文件位置;JSP基本语法;JSP指令;JSP指令—page指令;page指令用例;Page指令的属性;JSP声明;Java程序片段;JSP中的Java程序片段和模板文本;查看对应的servlet代码;练习;Java表达式;JSP中的注释...
JSP页面第一次被请求时,会被JSP引擎转译成Servlet的Java文件,然后再被编译成字节码文件执行。JSP指令标记为JSP页面转译提供整个页面的相关信息。 include指令用于在JSP页面静态插入一个文件,被插入的文件可以...
How to get the C language program in Delphi language 如何将C语言程序转译成delphi语言程序
JSP程序设计上机实验与综合实训,例子多多,代码全全
通过微软语音转译API 将说话人的语音转换成文字
(4)JSP引擎将JSP页面文件转译为Servlet源代码(相应的.j
JSP中九个隐含对象 一、page 对象 page对象代表JSP本身,更准确地说它代表JSP被转译后的Servlet,它可以调用Servlet类所定义的方法
PDF文件转译器 PDFtoWordConverter
这是脚手架及html转译GUI工具WinMac版。有需要的下载
Hibernate 转译中文版。 好久前收集的学习资料。
Propedit(Properties转译),将Properties文件中字符转译成中文,保存方式依然为字符。 方法:将文件解压后得到 propedit 文件夹--放入Eclipse或者MyEclipse的 dropins 文件夹中--重启应用即可。
JSP系列教材 (二)- 转译为Servlet JSP系列教材 (三)- 的7种页面元素 JSP系列教材 (四)- include包含其他页面的两种方式,以及其区别 JSP系列教材 (五)- 服务端跳转和客户端跳转 JSP系列教材 (六)- 中演示 ...
(4)JSP服务器将JSP代码转译为Servlet源代码。 (5)JSP服务器将Servlet源代码编译后执行。 (6)将产生的结果返回到客户端。 JSP程序设计实例教程(第2版)-电子课件第1章-Java-Web编程基础全文共73页,当前为第8页...
脚手架及html转译GUI工具WinMac版
有时候,是不是感觉自己写的JS不起作用,特别是拼装JS的时候,那么这个对你有作用..
emoji表情存储转译工具类 由于数据库不支持Emoji表情,为了避免改数据库编码格式代理来的风险。Java处理emoji 表情符号依赖的jar包 示例地址:https://pxy.iteye.com/blog/2435738
图像的转译与美术的释读.pdf
商业应用版收费,但功能不受限制,可通过转译VB6.0源代码为易语言4.14源代码,加快软件移植和开发过程,但请勿侵犯他人软件版权!如果有需要,请与作者联系购买! 本软件采用易语言编写,需要本软件源代码也可与...
用于Unicode中文的转译 用于Unicode中文的转译 用于Unicode中文的转译用于Unicode中文的转译
下面小编就为大家分享一篇解决JSON.stringify()自动将中文转译成unicode的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧