您的位置:首页 > Web前端 > JavaScript

JSP 编译和运行过程与JSP源码简单分析

2012-04-26 17:58 489 查看
原文链接

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>


当部署好 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);
}
}
}


当 JSP 页面被转译成Servlet时,内容主要包含三个部分:

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");


//通过 response响应设置返回客户端的页面属性

第二部分: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对象 向客户端写HTML标签

第三部分:声明的对象
<%
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());

第四部分:表达式

<%=str %>

out.print(str ); //写 即打印str变量的值
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: