新聞中心
很多人都會(huì)認(rèn)為JSP的執(zhí)行性能會(huì)和Servlet相差很多,其實(shí)執(zhí)行性能上的差別只在***次的執(zhí)行。因?yàn)镴SP在執(zhí)行***次后,會(huì)被編譯成Servlet的類(lèi)文件,即.class,當(dāng)再重復(fù)調(diào)用執(zhí)行時(shí),就直接執(zhí)行***次所產(chǎn)生的Servlet,而不再重新把JSP編譯成Servelt。

因此,除了***次的編譯會(huì)花較久的時(shí)間之外,之后JSP和Servlet的執(zhí)行速度就幾乎相同了。Web容器處理JSP文件請(qǐng)求的執(zhí)行過(guò)程主要包括以下4個(gè)部分:
1.客戶端發(fā)出Request請(qǐng)求
2.JSP Container 將JSP轉(zhuǎn)譯成Servlet的源代碼
3.將產(chǎn)生的Servlet源代碼經(jīng)過(guò)編譯后,并加載到內(nèi)存執(zhí)行
4.把結(jié)果Response(響應(yīng))至客戶端
在執(zhí)行JSP網(wǎng)頁(yè)時(shí),通??梢苑譃閮蓚€(gè)時(shí)期:轉(zhuǎn)譯時(shí)期(Translation Time)和請(qǐng)求時(shí)期(Request Time)。
◆轉(zhuǎn)譯時(shí)期:JSP網(wǎng)頁(yè)轉(zhuǎn)移成Servlet類(lèi)。
◆請(qǐng)求時(shí)期:Servlet類(lèi)執(zhí)行后,響應(yīng)結(jié)果至客戶端。
轉(zhuǎn)譯期間做了兩件事情:
◆轉(zhuǎn)譯時(shí)期:將JSP網(wǎng)頁(yè)轉(zhuǎn)移為Servlet源代碼 .java.
◆編譯時(shí)期:將Servlet 源代碼 .java編譯成 Servlet類(lèi) .class.
當(dāng)JSP網(wǎng)頁(yè)在執(zhí)行時(shí),JSP Container會(huì)做檢查工作,如果發(fā)現(xiàn)JSP網(wǎng)頁(yè)有更新修改時(shí),JSP Container才會(huì)再次編譯JSP成Servlet; 如果JSP沒(méi)有更新時(shí),就直接執(zhí)行前面所產(chǎn)生的Servlet。
- (showdate.jsp)
- <%@ page language="java" contentType="text/html;charset=gb2312" import="java.text.*,java.util.*;"%>
Show time - Hello :
- <%
- SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
- String str = format.format(new Date());
- %>
- <%=str %>
當(dāng)部署好 showdate.jsp之后,啟動(dòng)Tomcat服務(wù)器。
1.在IE瀏覽器中輸入配置好的路徑 .... showdate.jsp 請(qǐng)求這個(gè)頁(yè)面。
2.JSP Container 即Tomcat 服務(wù)器會(huì)將 showdate.jsp 轉(zhuǎn)譯成 showdate_jsp.java 源文件。
3.同時(shí)將 showdate_jsp.java 源文件編譯成 showdate_jsp.class。
4.編譯執(zhí)行showdate_jsp.class 類(lèi),處理請(qǐng)求,返回響應(yīng),容器將生成的頁(yè)面返回給客戶端顯示。
- (轉(zhuǎn)移成的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("\r\n");
- out.write("\r\n");
- out.write("
Show time \r\n");- out.write("\r\n");
- out.write(" \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("\r\n");
- out.write("");
- } 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);
- }
- }
- }
當(dāng)JSP頁(yè)面被轉(zhuǎn)譯成Servlet時(shí),內(nèi)容主要包含三個(gè)部分:
- public void _jspInit(){ ..}
- -- 當(dāng)JSP網(wǎng)頁(yè)一開(kāi)始執(zhí)行時(shí),***執(zhí)行此方法,執(zhí)行初始化工作
- public void _jspDestory(){...} – JSP網(wǎng)頁(yè)***執(zhí)行的方法
- public void _jspService(HttpServletRequest request, HttpServletResponse response)
- throws java.io.IOException, ServletException {
JSP網(wǎng)頁(yè)中最主要的程序都是在此執(zhí)行,將showdate.jsp和showdate_jsp.java做一個(gè)簡(jiǎn)單對(duì)比:
***部分:頁(yè)面屬性的對(duì)比
- <%@ page language="java" contentType="text/html;charset=gb2312" %>
- response.setContentType("text/html;charset=gb2312");
- //通過(guò) response響應(yīng)設(shè)置返回客戶端的頁(yè)面屬性
第二部分:HTML標(biāo)簽
Show time - ..
- out.write("\r\n");
- out.write("\r\n");
- out.write("\r\n");
- out.write("
Show time \r\n");- out.write("\r\n");
- out.write(" \r\n");
- out.write("\tHello : \r\n");
- out.write("\t");
- //通過(guò) out對(duì)象 向客戶端寫(xiě)HTML標(biāo)簽
第三部分:聲明的對(duì)象
- <%
- 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());
第四部分:表達(dá)式
- <%=str %>
- out.print(str ); //寫(xiě)即打印str變量的值
網(wǎng)頁(yè)題目:JSP轉(zhuǎn)譯成Servlet詳細(xì)過(guò)程
本文來(lái)源:http://fisionsoft.com.cn/article/ccedhge.html


咨詢
建站咨詢
