最近翻出大學時縮印版的jsp與servlet教材,紙質泛黃,如古董一般!粗略翻閱幾章,大感快慰,感覺不再像學校時那般晦澀難懂,看來自己果然是成長了,哈哈……書籍真是每看一遍都會有不同的收穫與感悟!溫故知新,注重基礎,天天向上!
借書上這幅圖闡述jsp執行原理,感覺流程分明、結構清晰、步驟詳實。
通過上圖,可以說對jsp執行原理是一目瞭然,這裡主要研究一下jsp檔案轉換為java檔案後執行的詳細情況。
我們都知道jsp其本質就是servlet,jsp首次請求時會被jsp引擎(如Tomcat)轉換為與其相應的java檔案,即servlet,故其執行過程與servlet的執行一樣。因此,我們不難推斷,它具有servlet的基本的屬性與方法,也就是說轉換後的java類是直接或間接地繼承自HttpServlt。這裡以原始碼為證,瞭解其本質,見下面程式碼片test1.jsp和test1_jsp.java。
我們先看看類test1_jsp.java的宣告,可知其繼承自HttpJspBase
public final class test1_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {
我們跟進一下可發現HttpJspBase繼承HttpServlet,在這裡我們可看到_jspInit(),_jspDestroy()和_jspService的宣告。當jsp檔案對應的servlet被例項化後,_jspInit()方法就會執行,_jspInit()在servlet的生命週期中只會被執行一次。_jspService()方法被呼叫來處理客戶端的請求。對每一個請求,JSP引擎建立一個新的執行緒來處理該請求。如果有多個客戶端同時請求該JSP檔案,則JSP引擎會建立多個執行緒。每個客戶端請求對應一個執行緒。以多執行緒方式執行可以大大降低對系統的資源需求,提高系統的併發量及響應時間。但不過也應該注意多執行緒的程式設計限制,由於該Servlet始終駐於記憶體,所以響應是非常快的。
public abstract class HttpJspBase extends HttpServlet
implements HttpJspPage
{
public final void init(ServletConfig config)
throws ServletException
{
super.init(config);
jspInit();
_jspInit();
}
public String getServletInfo() {
return Localizer.getMessage("jsp.engine.info");
}
public final void destroy() {
jspDestroy();
_jspDestroy();
}
public final void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
_jspService(request, response);
}
public void jspInit()
{
}
public void _jspInit()
{
}
public void jspDestroy()
{
}
protected void _jspDestroy()
{
}
public abstract void _jspService(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
throws ServletException, IOException;
}
在_jspService中我們可以看到這樣一段程式碼
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 {
response.setContentType("text/html;charset=ISO-8859-1");
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;複製程式碼
這讓我不難聯想到jsp的9個內建物件,我們繼續探究pageContext可發現jsp的內建物件都可從pageContext中獲得,因此,在jsp頁面上我們不用宣告便可直接使用request、response、session、spplication等,這也是它們為什麼叫jsp內建物件的原因所在。
jsp檔案:test1.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
This is my JSP page.
轉換後的servlet檔案:test1_jsp.java
package org.apache.jsp;
import javax.servlet.;
import javax.servlet.http.;
import javax.servlet.jsp.;
import java.util.;
public final class test1_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {
private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();
private static java.util.List _jspx_dependants;
private javax.el.ExpressionFactory _el_expressionfactory;
private org.apache.AnnotationProcessor _jsp_annotationprocessor;
public Object getDependants() {
return _jspx_dependants;
}
public void _jspInit() {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
_jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());
}
public void _jspDestroy() {
}
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException {
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 {
response.setContentType("text/html;charset=ISO-8859-1");
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');
out.write('\n');複製程式碼
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
out.write('\r');
out.write('\n');
out.write("\r\n");
out.write("\r\n");
out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");
out.write("<html>\r\n");
out.write(" <head>\r\n");
out.write(" <base href=\"");
out.print(basePath);
out.write("\">\r\n");
out.write(" \r\n");
out.write(" <title>My JSP 'test1.jsp' starting page</title>\r\n");
out.write(" \r\n");
out.write("\t<meta http-equiv=\"pragma\" content=\"no-cache\">\r\n");
out.write("\t<meta http-equiv=\"cache-control\" content=\"no-cache\">\r\n");
out.write("\t<meta http-equiv=\"expires\" content=\"0\"> \r\n");
out.write("\t<meta http-equiv=\"keywords\" content=\"keyword1,keyword2,keyword3\">\r\n");
out.write("\t<meta http-equiv=\"description\" content=\"This is my page\">\r\n");
out.write("\t<!--\r\n");
out.write("\t<link rel=\"stylesheet\" type=\"text/css\" href=\"styles.css\">\r\n");
out.write("\t-->\r\n");
out.write("\r\n");
out.write(" </head>\r\n");
out.write(" \r\n");
out.write(" <body>\r\n");
out.write(" This is my JSP page. <br>\r\n");
out.write(" </body>\r\n");
out.write("</html>\r\n");
} catch (Throwable t) {
if (!(t instanceof SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
try { out.clearBuffer(); } catch (java.io.IOException e) {}
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}複製程式碼
}
}