运行原理
如果JSP页面是第一次被请求运行,服务器的JSP编译器会生成JSP页面对应的JAVA代码,并且编译成类文件。当服务器再次收到对这个JSP页面请求的时候,会判断这个JSP页面是否被修改过,如果被修改过就会重新生成Java代码并且重新编译,而且服务器中的垃圾回收方法会把没用的类文件删除。如果没有修改过,服务器就会直接调用以前已经编译过的类文件。
举个例子,代码如下:
- <%@ 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代码如下:
- /*
- * 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在众多的动态语言中成为开发人员最喜欢的语言之一。