JSP動(dòng)態(tài)網(wǎng)頁(yè)開(kāi)發(fā)原理詳解
一、什么是JSP?
JSP全稱是Java Server Pages,它和servle技術(shù)一樣,都是SUN公司定義的一種用于開(kāi)發(fā)動(dòng)態(tài)web資源的技術(shù)。
JSP這門(mén)技術(shù)的最大的特點(diǎn)在于,寫(xiě)jsp就像在寫(xiě)html,但它相比html而言,html只能為用戶提供靜態(tài)數(shù)據(jù),而Jsp技術(shù)允許在頁(yè)面中嵌套java代碼,為用戶提供動(dòng)態(tài)數(shù)據(jù)。
二、JSP原理
2.1、Web服務(wù)器是如何調(diào)用并執(zhí)行一個(gè)jsp頁(yè)面的?
瀏覽器向服務(wù)器發(fā)請(qǐng)求,不管訪問(wèn)的是什么資源,其實(shí)都是在訪問(wèn)Servlet,所以當(dāng)訪問(wèn)一個(gè)jsp頁(yè)面時(shí),其實(shí)也是在訪問(wèn)一個(gè)Servlet,服務(wù)器在執(zhí)行jsp的時(shí)候,首先把jsp翻譯成一個(gè)Servlet,所以我們?cè)L問(wèn)jsp時(shí),其實(shí)不是在訪問(wèn)jsp,而是在訪問(wèn)jsp翻譯過(guò)后的那個(gè)Servlet,例如下面的代碼:
index.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"><html> <head> <base href="<%=basePath%>" rel="external nofollow" > <title>First Jsp</title> </head> <body> <% out.print("Hello Jsp"); %> </body></html>
當(dāng)我們通過(guò)瀏覽器訪問(wèn)index.jsp時(shí),服務(wù)器首先將index.jsp翻譯成一個(gè)index_jsp.class,在Tomcat服務(wù)器的work\Catalina\localhost\項(xiàng)目名\org\apache\jsp目錄下可以看到index_jsp.class的源代碼文件index_jsp.java,index_jsp.java的代碼如下:
package org.apache.jsp;import javax.servlet.*;import javax.servlet.http.*;import javax.servlet.jsp.*;import java.util.*;public final class index_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=UTF-8"); 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\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>First Jsp</title>\r\n"); out.write("\t\r\n"); out.write(" </head>\r\n"); out.write(" \r\n"); out.write(" <body>\r\n"); out.write(" "); out.print("Hello Jsp"); out.write("\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); } }}
我們可以看到,index_jsp這個(gè)類是繼承 org.apache.jasper.runtime.HttpJspBase這個(gè)類的,通過(guò)查看Tomcat服務(wù)器的源代碼,可以知道在apache-tomcat-6.0.20-src\java\org\apache\jasper\runtime目錄下存HttpJspBase這個(gè)類的源代碼文件,如下圖所示:
我們可以看看HttpJsBase這個(gè)類的源代碼,如下所示:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.jasper.runtime;import java.io.IOException;import javax.servlet.ServletConfig;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.jsp.HttpJspPage;import javax.servlet.jsp.JspFactory;import org.apache.jasper.compiler.Localizer;/** * This is the super class of all JSP-generated servlets. * * @author Anil K. Vijendran */public abstract class HttpJspBase extends HttpServlet implements HttpJspPage{ protected HttpJspBase() { } 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(); } /** * Entry point into service. */ 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 request, HttpServletResponse response) throws ServletException, IOException;}
HttpJspBase類是繼承HttpServlet的,所以HttpJspBase類是一個(gè)Servlet,而index_jsp又是繼承HttpJspBase類的,所以index_jsp類也是一個(gè)Servlet,所以當(dāng)瀏覽器訪問(wèn)服務(wù)器上的index.jsp頁(yè)面時(shí),其實(shí)就是在訪問(wèn)index_jsp這個(gè)Servlet,index_jsp這個(gè)Servlet使用_jspService這個(gè)方法處理請(qǐng)求。
2.2、Jsp頁(yè)面中的html排版標(biāo)簽是如何被發(fā)送到客戶端的?
瀏覽器接收到的這些數(shù)據(jù)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="http://localhost:8080/JavaWeb_Jsp_Study_20140603/" rel="external nofollow" > <title>First Jsp</title> </head> <body> Hello Jsp </body></html>
都是在_jspService方法中使用如下的代碼輸出給瀏覽器的:
out.write("\r"); out.write("\n");String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 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>First Jsp</title>\r\n"); out.write("\t\r\n"); out.write(" </head>\r\n"); out.write(" \r\n"); out.write(" <body>\r\n"); out.write(" "); out.print("Hello Jsp"); out.write("\r\n"); out.write(" </body>\r\n"); out.write("</html>\r\n");
在jsp中編寫(xiě)的java代碼和html代碼都會(huì)被翻譯到_jspService方法中去,在jsp中編寫(xiě)的java代碼會(huì)原封不動(dòng)地翻譯成java代碼,如<%out.print("Hello Jsp");%>直接翻譯成out.print("Hello Jsp");,而HTML代碼則會(huì)翻譯成使用out.write("<html標(biāo)簽>\r\n");的形式輸出到瀏覽器。在jsp頁(yè)面中編寫(xiě)的html排版標(biāo)簽都是以out.write("<html標(biāo)簽>\r\n");的形式輸出到瀏覽器,瀏覽器拿到html代碼后才能夠解析執(zhí)行html代碼。
2.3、Jsp頁(yè)面中的java代碼服務(wù)器是如何執(zhí)行的?
在jsp中編寫(xiě)的java代碼會(huì)被翻譯到_jspService方法中去,當(dāng)執(zhí)行_jspService方法處理請(qǐng)求時(shí),就會(huì)執(zhí)行在jsp編寫(xiě)的java代碼了,所以Jsp頁(yè)面中的java代碼服務(wù)器是通過(guò)調(diào)用_jspService方法處理請(qǐng)求時(shí)執(zhí)行的。
2.4、Web服務(wù)器在調(diào)用jsp時(shí),會(huì)給jsp提供一些什么java對(duì)象?
查看_jspService方法可以看到,Web服務(wù)器在調(diào)用jsp時(shí),會(huì)給Jsp提供如下的8個(gè)java對(duì)象
PageContext pageContext;HttpSession session;ServletContext application;ServletConfig config;JspWriter out;Object page = this;HttpServletRequest request,HttpServletResponse response
其中page對(duì)象,request和response已經(jīng)完成了實(shí)例化,而其它5個(gè)沒(méi)有實(shí)例化的對(duì)象通過(guò)下面的方式實(shí)例化
pageContext = _jspxFactory.getPageContext(this, request, response,null, true, 8192, true); application = pageContext.getServletContext(); config = pageContext.getServletConfig(); session = pageContext.getSession(); out = pageContext.getOut();
這8個(gè)java對(duì)象在Jsp頁(yè)面中是可以直接使用的,如下所示:
<% session.setAttribute("name", "session對(duì)象");//使用session對(duì)象,設(shè)置session對(duì)象的屬性 out.print(session.getAttribute("name")+"<br/>");//獲取session對(duì)象的屬性 pageContext.setAttribute("name", "pageContext對(duì)象");//使用pageContext對(duì)象,設(shè)置pageContext對(duì)象的屬性 out.print(pageContext.getAttribute("name")+"<br/>");//獲取pageContext對(duì)象的屬性 application.setAttribute("name", "application對(duì)象");//使用application對(duì)象,設(shè)置application對(duì)象的屬性 out.print(application.getAttribute("name")+"<br/>");//獲取application對(duì)象的屬性 out.print("Hello Jsp"+"<br/>");//使用out對(duì)象 out.print("服務(wù)器調(diào)用index.jsp頁(yè)面時(shí)翻譯成的類的名字是:"+page.getClass()+"<br/>");//使用page對(duì)象 out.print("處理請(qǐng)求的Servlet的名字是:"+config.getServletName()+"<br/>");//使用config對(duì)象 out.print(response.getContentType()+"<br/>");//使用response對(duì)象 out.print(request.getContextPath()+"<br/>");//使用request對(duì)象%>
運(yùn)行結(jié)果如下:
2.5、Jsp最佳實(shí)踐
Jsp最佳實(shí)踐就是jsp技術(shù)在開(kāi)發(fā)中該怎么去用。
不管是JSP還是Servlet,雖然都可以用于開(kāi)發(fā)動(dòng)態(tài)web資源。但由于這2門(mén)技術(shù)各自的特點(diǎn),在長(zhǎng)期的軟件實(shí)踐中,人們逐漸把servlet作為web應(yīng)用中的控制器組件來(lái)使用,而把JSP技術(shù)作為數(shù)據(jù)顯示模板來(lái)使用。其原因?yàn)椋绦虻臄?shù)據(jù)通常要美化后再輸出:讓jsp既用java代碼產(chǎn)生動(dòng)態(tài)數(shù)據(jù),又做美化會(huì)導(dǎo)致頁(yè)面難以維護(hù)。讓servlet既產(chǎn)生數(shù)據(jù),又在里面嵌套html代碼美化數(shù)據(jù),同樣也會(huì)導(dǎo)致程序可讀性差,難以維護(hù)。因此最好的辦法就是根據(jù)這兩門(mén)技術(shù)的特點(diǎn),讓它們各自負(fù)責(zé)各的,servlet只負(fù)責(zé)響應(yīng)請(qǐng)求產(chǎn)生數(shù)據(jù),并把數(shù)據(jù)通過(guò)轉(zhuǎn)發(fā)技術(shù)帶給jsp,數(shù)據(jù)的顯示jsp來(lái)做。
2.6、Tomcat服務(wù)器的執(zhí)行流程
第一次執(zhí)行:
- 客戶端通過(guò)電腦連接服務(wù)器,因?yàn)槭钦?qǐng)求是動(dòng)態(tài)的,所以所有的請(qǐng)求交給WEB容器來(lái)處理
- 在容器中找到需要執(zhí)行的*.jsp文件
- 之后*.jsp文件通過(guò)轉(zhuǎn)換變?yōu)?.java文件
- *.java文件經(jīng)過(guò)編譯后,形成*.class文件
- 最終服務(wù)器要執(zhí)行形成的*.class文件
第二次執(zhí)行:
因?yàn)橐呀?jīng)存在了*.class文件,所以不在需要轉(zhuǎn)換和編譯的過(guò)程
修改后執(zhí)行:
1.源文件已經(jīng)被修改過(guò)了,所以需要重新轉(zhuǎn)換,重新編譯。
到此這篇關(guān)于JSP動(dòng)態(tài)網(wǎng)頁(yè)開(kāi)發(fā)原理詳解的文章就介紹到這了,更多相關(guān)JSP動(dòng)態(tài)網(wǎng)頁(yè)開(kāi)發(fā)原理內(nèi)容請(qǐng)搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!
相關(guān)文章:
1. 教你怎么用JSP統(tǒng)計(jì)網(wǎng)站訪問(wèn)人數(shù)2. JSP實(shí)時(shí)顯示當(dāng)前系統(tǒng)時(shí)間的四種方式示例解析3. jsp+mysql實(shí)現(xiàn)網(wǎng)頁(yè)的分頁(yè)查詢4. Eclipse3.1+MyEclipse4.0+Lomboz3.1圖解安裝開(kāi)發(fā)JSP5. jsp Response對(duì)象頁(yè)面重定向、時(shí)間的動(dòng)態(tài)顯示6. jsp實(shí)現(xiàn)剪子石頭布小游戲7. JSP開(kāi)發(fā)之hibernate之單向多對(duì)一關(guān)聯(lián)的實(shí)例8. jsp response.sendRedirect()用法詳解9. jsp中文亂碼問(wèn)題的簡(jiǎn)單解決方法10. JSP使用過(guò)濾器防止Xss漏洞
