jsp拦截JspWriter实现类似php的ob_get_contents
php可以通过ob_start和ob_get_contents可以拦截并得到echo等方式输出的内容,并可以方便的进行二次处理。java的jsp也是可以非常方便就实现这样的功能,那就是实现一个自定义的JspWriter.
oscache的功能强大的jsp缓存标签就是这样干的。
前段时间作了个简单的rss输出,由于避免访问量过大影响到数据库和接口调用。
于是需要对生成的rss文本进行缓存。原有的jsp代码都已经写好,大概动就太麻烦了,于是用TextJspWriter继承javax.servlet.jsp.JspWriter,然后在jsp作小改造就搞定了。
php的用法参考: http://cn2.php.net/ob_get_contents
TextJspWriter.java的代码为:
[code]
package com.lizongbo.util;
import java.io.IOException;
import java.io.StringWriter;
import javax.servlet.jsp.JspWriter;
public class TextJspWriter extends JspWriter {
public String getString() {
return sb.toString();
}
private StringBuilder sb = null;
public TextJspWriter() {
this(8192, true);
}
public TextJspWriter(int bufferSize, boolean autoFlush) {
super(bufferSize, autoFlush);
sb=new StringBuilder(bufferSize);
}
@Override
public void clear() throws IOException {
sb = new StringBuilder();
}
@Override
public void clearBuffer() throws IOException {
sb = new StringBuilder();
}
@Override
public void close() throws IOException {
}
@Override
public void flush() throws IOException {
}
@Override
public int getRemaining() {
return 0;
}
@Override
public void newLine() throws IOException {
sb.append("\n");
}
@Override
public void print(boolean arg0) throws IOException {
sb.append(arg0);
}
@Override
public void print(char arg0) throws IOException {
sb.append(arg0);
}
@Override
public void print(int arg0) throws IOException {
sb.append(arg0);
}
@Override
public void print(long arg0) throws IOException {
sb.append(arg0);
}
@Override
public void print(float arg0) throws IOException {
sb.append(arg0);
}
@Override
public void print(double arg0) throws IOException {
sb.append(arg0);
}
@Override
public void print(char[] arg0) throws IOException {
sb.append(arg0);
}
@Override
public void print(String arg0) throws IOException {
sb.append(arg0);
}
@Override
public void print(Object arg0) throws IOException {
sb.append(arg0);
}
@Override
public void println() throws IOException {
sb.append("\n");
}
@Override
public void println(boolean arg0) throws IOException {
sb.append(arg0);
println();
}
@Override
public void println(char arg0) throws IOException {
sb.append(arg0);
println();
}
@Override
public void println(int arg0) throws IOException {
sb.append(arg0);
println();
}
@Override
public void println(long arg0) throws IOException {
sb.append(arg0);
println();
}
@Override
public void println(float arg0) throws IOException {
sb.append(arg0);
println();
}
@Override
public void println(double arg0) throws IOException {
sb.append(arg0);
println();
}
@Override
public void println(char[] arg0) throws IOException {
sb.append(arg0);
println();
}
@Override
public void println(String arg0) throws IOException {
sb.append(arg0);
println();
}
@Override
public void println(Object arg0) throws IOException {
sb.append(arg0);
println();
}
@Override
public void write(char[] cbuf, int off, int len) throws IOException {
sb.append(cbuf, off, len);
}
}
[/code]
jsp代码:
[code]
《%//读取缓存
String textStr==rssTextCache.get(cacheKey);
if(textStr==null){
JspWriter oldout=out;
out= new com.lizongbo.util.TextJspWriter();
%》
《% //正常输出rss内容%》
《%
textStr=((com.lizongbo.util.TextJspWriter)out).getString();
out=oldout;//放入缓存中
rssTextCache.put(cacheKey,textStr);
}
%》
《%=textStr%》
[/code]
Tags: Java, jsp, JspWriter, php, 缓存标签: Java, jsp, JspWriter, php, 缓存
或分享到 Google Buzz
或
或