JSP运行原理及优势
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 =
&n
补充:Web开发 , Jsp ,