JSP本質上就是把Java代碼嵌套到HTML中,然後經過JSP容器(Tomcat、Resin、Weblogic等)的編譯執行,再根據這些動態代碼的運行結果生成對應的HTML代碼,從而可以在客戶端的浏覽器中正常顯示。
運行原理
如果JSP頁面是第一次被請求運行,服務器的JSP編譯器會生成JSP頁面對應的JAVA代碼,並且編譯成類文件。當服務器再次收到對這個JSP頁面請求的時候,會判斷這個JSP頁面是否被修改過,如果被修改過就會重新生成Java代碼並且重新編譯,而且服務器中的垃圾回收方法會把沒用的類文件刪除。如果沒有修改過,服務器就會直接調用以前已經編譯過的類文件。
舉個例子,代碼如下:
[html]
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<html>
<head>
<title>簡單JSP頁面示例</title>
</head>
<body>
這是一個簡單的 JSP頁面示例 <br>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<html>
<head>
<title>簡單JSP頁面示例</title>
</head>
<body>
這是一個簡單的 JSP頁面示例 <br>
</body>
</html>
上面這個JSP頁面在被請求的時候,Web服務器中JSP編譯器會生成對應的Java文件,上面這個JSP程序在服務器中生成的對應Java代碼如下:
[java]
/*
* Generated by the Jasper component of Apache Tomcat
* Version: Apache Tomcat/7.0.22
* Generated at: 2012-03-22 08:40:10 UTC
* Note: The last modified time of this file was set to
* the last modified time of the source file after
* generation to assist with modification tracking.
*/
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.*;
public final class Simple_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {
private static final javax.servlet.jsp.JspFactory _jspxFactory =
javax.servlet.jsp.JspFactory.getDefaultFactory();
private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;
private javax.el.ExpressionFactory _el_expressionfactory;
private org.apache.tomcat.InstanceManager _jsp_instancemanager;
public java.util.Map<java.lang.String,java.lang.Long> getDependants() {
return _jspx_dependants;
}
public void _jspInit() {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
_jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
}
public void _jspDestroy() {
}
public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
throws java.io.IOException, javax.servlet.ServletException {
final javax.servlet.jsp.PageContext pageContext;
javax.servlet.http.HttpSession session = null;
final javax.servlet.ServletContext application;
final javax.servlet.ServletConfig config;
javax.servlet.jsp.JspWriter out = null;
final java.lang.Object page = this;
javax.servlet.jsp.JspWriter _jspx_out = null;
javax.servlet.jsp.PageContext _jspx_page_context = null;
try {
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>簡單JSP頁面示例</title>\r\n");
out.write(" </head>\r\n");
out.write("<body>\r\n");
out.write(" 這是一個簡單的 JSP頁面示例 <br>\r\n");
out.write(" </body>\r\n");
out.write("</html>\r\n");
} catch (java.lang.Throwable t) {
if (!(t instanceof javax.servlet.jsp.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);
}
}
}
/*
* Generated by the Jasper component of Apache Tomcat
* Version: Apache Tomcat/7.0.22
* Generated at: 2012-03-22 08:40:10 UTC
* Note: The last modified time of this file was set to
* the last modified time of the source file after
* generation to assist with modification tracking.
*/
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.*;
public final class Simple_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {
private static final javax.servlet.jsp.JspFactory _jspxFactory =
javax.servlet.jsp.JspFactory.getDefaultFactory();
private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;
private javax.el.ExpressionFactory _el_expressionfactory;
private org.apache.tomcat.InstanceManager _jsp_instancemanager;
public java.util.Map<java.lang.String,java.lang.Long> getDependants() {
return _jspx_dependants;
}
public void _jspInit() {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
_jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
}
public void _jspDestroy() {
}
public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
throws java.io.IOException, javax.servlet.ServletException {
final javax.servlet.jsp.PageContext pageContext;
javax.servlet.http.HttpSession session = null;
final javax.servlet.ServletContext application;
final javax.servlet.ServletConfig config;
javax.servlet.jsp.JspWriter out = null;
final java.lang.Object page = this;
javax.servlet.jsp.JspWriter _jspx_out = null;
javax.servlet.jsp.PageContext _jspx_page_context = null;
try {
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>簡單JSP頁面示例</title>\r\n");
out.write(" </head>\r\n");
out.write("<body>\r\n");
out.write(" 這是一個簡單的 JSP頁面示例 <br>\r\n");
out.write(" </body>\r\n");
out.write("</html>\r\n");
} catch (java.lang.Throwable t) {
if (!(t instanceof javax.servlet.jsp.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);
}
}
}
上面這段代碼就是剛才那個JSP所對應的Java代碼,如果你用的服務器為Tomcat那麼這個Java代碼所在的位置為Tomcat(我的版本為7.0)目錄下的work\Catalina\localhost\你的項目名字\org\apache\jsp中。在這個目錄下應該有兩個對應的文件,一個是class文件一個是java文件。
上面這段程序本質就是一個servelet,它把所有頁面的顯示內容都用out對象打印出來,包括每個HTML標簽,所以說JSP頁面本質上就是Servelet的一種化身,在JSP程序中離不開Servelet的影子。這段代碼的具體語法可以不必深究,這些工作都是由服務器中的JSP編譯器來完成,這個過程是自動完成的,無需手動干預。
需要注意的是只有被請求過的頁面才能生成對應的Java文件,沒有請求的頁面會在第一次請求的時候生成Java文件,當JSP頁面被修改後,再次對這個頁面進行請求才會重新生成對應的Java文件。
JSP的優勢
JSP就是在HTML中嵌入Java代碼,所以在本質上JSP程序就是Java程序,JSP程序繼承了Java的一切優點。JSP程序有嚴格的Java語法和豐富的Java類庫支持。
JSP頁面在服務器中都會被JSP編譯器編譯成對應的Servlet,所以就擁有Java跨平台的優點,所有的JSP程序,無需改動就可以方便的遷移到其他操作系統平台,這就是在其他動態腳本中所無法想象的。
JSP中可以使用JavaBean進行邏輯封裝,這樣就可以實現邏輯功能代碼的重用,從而大大提高系統的可重用性,同時也提高了程序的開發效率。
JSP程序容易上手,如果有HTML和Java的基本知識,那麼學習JSP程序就沒有任何難度。
在JSP中可以使用Java眾多的開源工具也是其他的動態網頁語言無法比擬的。
由於以上種種優勢JSP在眾多的動態語言中成為開發人員最喜歡的語言之一。