一个在JSP下实现将动态页面转为静态的方案

2008-02-23 07:52:45来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

  1.前言

  为了能深入浅出的理解这个框架的由来,我们首先来了解一下JSP解析器将我们写的JSP代码转换成的JAVA文件的内容。

  下面是一个JSP文件test.jsp

<%@ page language="java" contentType="text/html;charset=GB2312" %>
<%
out.write("<!--文件开始-->");
%>
<html>
<head>
<body>
<%= "输出"%>
</body>
</head>
</html>

  经过TOMCAT转换出的JAVA文件test$jsp.java内容如下:

package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import org.apache.jasper.runtime.*;
public class test$jsp extends HttpJspBase {
static {
}
public testOutRedir$jsp( ) {
}
private static boolean _jspx_inited = false;
public final void _jspx_init() throws org.apache.jasper.runtime.JspException {
}
public void _jspService(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=GB2312");
pageContext = _jspxFactory.getPageContext(this, request, response,
"", true, 8192, true);
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
//为了节省篇幅,我删除了解释器添加的注释
out.write("\r\n");
//上一句是由于<%@ page language="java" contentType="text/html;charset=GB2312" %>后面的换行产生的
out.write("<!--文件开始-->");
out.write("\r\n<html>\r\n<head>\r\n<body>\r\n");
out.print( "输出" );
out.write("\r\n</body>\r\n</head>\r\n</html>\r\n");
} catch (Throwable t) {
if (out != null && out.getBufferSize() != 0)
out.clearBuffer();
if (pageContext != null) pageContext.handlePageException(t);
} finally {
if (_jspxFactory != null) _jspxFactory.releasePageContext(pageContext);
}
}
}

  从上面的代码中可以清晰的看到JSP内建的几个对象(out、request、response、session、pageContext、application、config、page)是怎么产生的,懂servlet的朋友一看就能明白。

  下面重点理解一下out对象,它被声明为JspWriter类型,JspWriter是一个抽象类,在包javax.servlet.jsp中可以找到它的定义。

abstract public class javax.servlet.jsp.JspWriter extends java.io.Writer{
final public static int NO_BUFFER = 0;
final public static int DEFAULT_BUFFER = -1;
final public static int UNBOUNDED_BUFFER = -2;
protected int bufferSize;
protected Boolean autoFlush;
protected javax.servlet.jsp.JspWriter(int arg1, boolean arg2);
abstract public void newLine() throws IOException ;
abstract public void print(boolean arg0) throws IOException ;
abstract public void print(char arg0) throws IOException ;
abstract public void print(int arg0) throws IOException ;
abstract public void print(long arg0) throws IOException ;
abstract public void print(float arg0) throws IOException ;
abstract public void print(double arg0) throws IOException ;
abstract public void print(char<> arg0) throws IOException ;
abstract public void print(String arg0) throws IOException ;
abstract public void print(Object arg0) throws IOException ;
abstract public void println() throws IOException ;
abstract public void println(boolean arg0) throws IOException ;
abstract public void println(char arg0) throws IOException ;
abstract public void println(int arg0) throws IOException ;
abstract public void println(long arg0) throws IOException ;
abstract public void println(float arg0) throws IOException ;
abstract public void println(double arg0) throws IOException ;
abstract public void println(char<> arg0) throws IOException ;
abstract public void println(String arg0) throws IOException ;
abtract public void println(Object arg0) throws IOException ;
abstract public void clear() throws IOException ;

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:在JSP开发中使用jdom解析临时存放数据的XML文件

下一篇:Jsp开发环境配置全过程