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 ;
abstract public void clearbuffer() throws ioexception ;
abstract public void flush() throws ioexception ;
abstract public void close() throws ioexception ;
public int getbuffersize() ;
abstract public int getremaining() ;
public boolean isautoflush() ;
}
我相信当我写到这里你可能已经知道我想怎么做了。是的,来个偷天换日,继承jspwriter类,然后实现其定义的虚函数,然后把out变量替换成你自己实现的类的实例就ok了。
2.实现替换
假设:
<%@ page language="java" contenttype="text/html;charset=gb2312"
import="jwb.util.htmlintofile,jwb.util.tempsinglet,java.io.file"%><%
jspwriter out_bak = out;
string arg1="argument1";
string filepath = "/cache/根据参数生成文件名_" + arg1 + ".html";
//首先判断文件是否已经存在,如果不存在则执行本页面,否则跳转到静态页面就ok了
file f = new file(pagecontext.getservletcontext().getrealpath(filepath));
if(f.exists())
{
out_bak.clear();
pagecontext.forward(filepath);
system.out.println("直接转到静态页面");
return;
}
out = new htmlintofile(pagecontext.getservletcontext().getrealpath(filepath));
out.write("<!--文件开始-->");
%>
<html>
<head>
<body>
<%= "看吧,这就是输出被重定向到文件的实现,很简单吧^_^"%>
</body>
</head>
</html>
<%
out.close();//关闭生成的静态文件
out_bak.clear();
pagecontext.forward(filepath);
system.out.println("执行本页面后再转到静态页面");
return;
%>
3.更新问题
下面就讨论一下如何更新生成静态文件,其实从上面实现中你可以看到,很简单的就是将生成的静态文件删除即可,至于什么时候删除,要看你的需求了。我能想到的几种情况如下:
当用来生成页面的数据更新时
如果不需要很提供时时的数据可以定时更新
永远不更新
新闻热点
疑难解答