如何從response里面取出向客戶端輸出的html流
Posted on 2005-08-17 14:40 publisher luo 閱讀(3688) 評論(0) 編輯 收藏 所屬分類: 項目問題解決項目里需要在把servlet,jsp生成的html代碼存儲到數據庫中。如何解決,比較直接的想法是客戶端用xmlhttp,或者直接在客戶端js代碼里發出請求,然后把得到的html代碼在作為提交數據發送給服務器端,由服務器端程序接受并存入數據庫中。
但是此方法需要耗費兩次網絡傳輸,肯定性能不加,而且處理起來要幾塊程序同時協作才行。還是想辦法從服務器端直接獲取。因為從response無法直接得到輸出流,得想其他的辦法。一種是干脆在服務器端寫一個監控socket接口的客戶端程序,或者用httpunit幫助完成,就是把客戶端程序移到服務器端執行。還是相對比較復雜,能不能從response入手?
答案是肯定的,采用response代理來截獲response的幾個輸出函數,然后存儲起來,已備查詢。
靈感來自于前一陣一直研究的java動態代理機制(現在應用在spring的aop實現中),此處不用動態代理,就使用靜態代理,proxy模式就足夠了。
靈感來自于前一陣一直研究的java動態代理機制(現在應用在spring的aop實現中),此處不用動態代理,就使用靜態代理,proxy模式就足夠了。
分別實現三個代理類:ServletResponseProxy,ServletOutputStreamProxy,PrintWriterProxy
Responseproxy 主要代碼:
public class ServletResponseProxy implements HttpServletResponse {
private HttpServletResponse obj;//實際的HttpServletResponse 實例
private HttpServletResponse obj;//實際的HttpServletResponse 實例
public ServletResponseProxy(HttpServletResponse obj) {
this.obj = obj;
HtmlBuffer.cleanStr(); //情空緩存
}
this.obj = obj;
HtmlBuffer.cleanStr(); //情空緩存
}
//獲得outputStreamProxy
public ServletOutputStream getOutputStream() throws IOException {
ServletOutputStream so = obj.getOutputStream();
ServletOutputStreamProxy sop = new ServletOutputStreamProxy(so);
return sop;
}
public ServletOutputStream getOutputStream() throws IOException {
ServletOutputStream so = obj.getOutputStream();
ServletOutputStreamProxy sop = new ServletOutputStreamProxy(so);
return sop;
}
//獲得printWriterProxy
public PrintWriter getWriter() throws IOException {
PrintWriter pw = obj.getWriter();
PrintWriterProxy pwp = new PrintWriterProxy(pw);
return (PrintWriter) pwp;
}
}
public PrintWriter getWriter() throws IOException {
PrintWriter pw = obj.getWriter();
PrintWriterProxy pwp = new PrintWriterProxy(pw);
return (PrintWriter) pwp;
}
}
PrintWriterProxy:
public class PrintWriterProxy
extends PrintWriter {
private PrintWriter pw = null;
public PrintWriterProxy(PrintWriter pw) {
super(pw);
this.pw = pw;
}
extends PrintWriter {
private PrintWriter pw = null;
public PrintWriterProxy(PrintWriter pw) {
super(pw);
this.pw = pw;
}
//截獲寫內容寫入buffer
public void write(int c) {
char a = (char) c;
String s = new String(new char[] {a});
HtmlBuffer.addStr(s);
pw.write(c);
}
char a = (char) c;
String s = new String(new char[] {a});
HtmlBuffer.addStr(s);
pw.write(c);
}
}
ServletOutputStreamProxy:
public class ServletOutputStreamProxy
extends ServletOutputStream {
private ServletOutputStream obj;
extends ServletOutputStream {
private ServletOutputStream obj;
public ServletOutputStreamProxy(ServletOutputStream obj){
this.obj = obj;
}
this.obj = obj;
}
//截獲寫內容寫入buffer
public void write(int b) throws IOException {
Integer it = new Integer(b);
HtmlBuffer.addStr(new String(new byte[]{it.byteValue()}));
obj.write(b);
}
Integer it = new Integer(b);
HtmlBuffer.addStr(new String(new byte[]{it.byteValue()}));
obj.write(b);
}
}
由于web Httpserver 是多線程執行服務端程序,所以buffer應該分線程來存取,這樣大家才能不互相干擾。所以buffer需要實現TreadLocal接口。
HtmlBuffer代碼簡單實現如下:
public class HtmlBuffer {
private static class HtmlInfo extends ThreadLocal {
private Map values = Collections.synchronizedMap(new HashMap());
private Map values = Collections.synchronizedMap(new HashMap());
public Object initialValue() {
return new String();
}
return new String();
}
public String getHtmlStr() {
return (String) this.get();
}
return (String) this.get();
}
public Object get() {
Thread curThread = Thread.currentThread();
Object o = values.get(curThread);
if (o == null && !values.containsKey(curThread)) {
o = initialValue();
values.put(curThread, o);
}
return o;
}
Thread curThread = Thread.currentThread();
Object o = values.get(curThread);
if (o == null && !values.containsKey(curThread)) {
o = initialValue();
values.put(curThread, o);
}
return o;
}
public void set(Object newValue) {
values.put(Thread.currentThread(), newValue);
}
}
values.put(Thread.currentThread(), newValue);
}
}
private static HtmlInfo htmlInfo = new HtmlInfo();
public static void cleanStr(){
htmlInfo.set( "");
}
public static void cleanStr(){
htmlInfo.set( "");
}
public static void addStr(String htmlStr) {
String htmlstr = (String)htmlInfo.get();
if(htmlstr == null) htmlstr ="";
htmlstr += htmlStr;
htmlInfo.set( htmlstr);
}
String htmlstr = (String)htmlInfo.get();
if(htmlstr == null) htmlstr ="";
htmlstr += htmlStr;
htmlInfo.set( htmlstr);
}
public static String getStr() {
return (String)htmlInfo.get();
}
}
return (String)htmlInfo.get();
}
}