JSP页面翻译原理
【IT168 技术文档】众所周知,每一个JSP页面都会被Web容器编译成一个Java类,供Web容器调用,并且生成HTML叶面回馈给用户。而了解其中的变异方法和规则,对我们学习JSP是非常有好处的,可以说学习好了这个编译原理,就已经学习好了大部分的JSP知识,剩下的工作就只剩下熟记一些tablib和反复应用以使自己更加熟练而已了。
先来看一下JSP页面所对应的Class的基本结构。每一个JSP页面都会被编译成成如下的格式样子,先给一个大致的印象,详细的说明在后面:
public class My$jsp extends HttpJspBase {
static {}
public date$jsp() {}
private static boolean _jspx_inited = false;
public final void _jspx_init()
throws org.apache.jasper.runtime.JspException {};
public void _JSP pageservice(HttpServletRequest request,
HttpServletResponse response)
throws java.io.IOException, ServletException {
JspFactory _jspxFactory = null;
PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
String _value = null;
try {
if (_jspx_inited == false) {
synchronized (this) {
if (_jspx_inited == false) {
_jspx_init();
_jspx_inited = true;
}
}
}
_jspxFactory = JspFactory.getDefaultFactory();
response.setContentType("text/html;charset=ISO-8859-1");
pageContext = _jspxFactory.getPageContext(this, request, response,
"", true, 8192, true);
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
// HTML // begin [file="/date.jsp";from=(0,0);to=(7,6)]
out.write("<html>rn<head>rn<title>Date</title>rn" +
"</head>rn<body>rn<h3>rn" +
"The date isrn");
// end
// begin [file="/date.jsp";from=(7,8);to=(7,57)]
out.println((new java.util.Date()).toString());
// end
// HTML // begin [file="/date.jsp";from=(7,59);to=(10,7)]
out.write("rn </h3>rn </body>rn</html>");
// end
} catch (Throwable t) {
if (out != null && out.getBufferSize() != 0) {
out.clearBuffer();
}
if (pageContext != null) {
pageContext.handlePageException(t);
}
} finally {
if (_jspxFactory != null) {
_jspxFactory.releasePageContext(pageContext);
}
}
}
}