最近寫(xiě)個(gè)人網(wǎng)站,遇到一個(gè)問(wèn)題,如題,到網(wǎng)上查找最終找到的一種解決方法:
使用Filter實(shí)現靜態(tài)HTML緩沖(一種折中方法)
緩沖是Web應用中必須考慮的一個(gè)提高性能的重要手段。對于基于JSP/Servlet技術(shù)的站點(diǎn),常用的緩沖有持久層的數據庫連接池緩沖,內存中的值對象緩沖,JSP頁(yè)面緩沖,以及各種各樣的緩沖框架等等,無(wú)不是為了提高系統的吞吐量。 ¬[ @2v4Y p e5c P.k b
然而對于大型站點(diǎn)來(lái)說(shuō),將JSP頁(yè)面轉換為靜態(tài)Html也許是最高效的方法,特別適合于數據不經(jīng)常變化但是頁(yè)面訪(fǎng)問(wèn)量特別大的站點(diǎn),如新聞等,通過(guò)把JSP動(dòng)態(tài)頁(yè)面預先轉換為靜態(tài)Html頁(yè)面,當用戶(hù)請求此頁(yè)面時(shí),系統自動(dòng)導向到對應的Html頁(yè)面,從而避免解析JSP請求,調用后臺邏輯以及訪(fǎng)問(wèn)數據庫等操作所帶來(lái)的巨大開(kāi)銷(xiāo)。
如何將一個(gè)已有的JSP站點(diǎn)的動(dòng)態(tài)JSP頁(yè)面轉化為靜態(tài)Html呢?我們希望在不用更改現有Servlet,JSP的前提下讓系統自動(dòng)將這些JSP轉換為Html頁(yè)。幸運的是,Filter為我們提供了一種實(shí)現方案。 o'iK$T0K
Filter是Servlet 2.2規范中最激動(dòng)人心的特性。Filter能過(guò)濾特定URL如/admin/*并進(jìn)行必要的預處理,如修改Request和Response,從而實(shí)現定制的輸入輸出。更強大的是,Filter本身是一個(gè)責任鏈模式,它能一個(gè)接一個(gè)地傳遞下去,從而將實(shí)現不同功能的Filter串起來(lái),并且可以動(dòng)態(tài)組合。
要自動(dòng)生成靜態(tài)頁(yè)面,用Filter截獲jsp請求并先進(jìn)行預處理,自動(dòng)生成Html,是個(gè)不錯的主意。一個(gè)很容易想到的方法是在Filter截獲Request后,導向一個(gè)Servlet,在這個(gè)Servlet中向本機發(fā)送一個(gè)http請求,然后將響應寫(xiě)入一個(gè)文件:
B Q u s ]F/B5V ~:? Y)~
URLConnection urlConn = URLConnection.open(http://localhost/req);
3l T tL*} w ^
注意要避免遞歸。 K p-i8n f2F cN7^ f
7s s i A:a l*o A)~ ~ q
另一個(gè)方法是不模擬http,而是定制Response,把服務(wù)器返回的JSP響應輸出到我們自己的Response中,就可以將響應快速寫(xiě)入Html文件,然后再發(fā)送給客戶(hù)。而且,由于沒(méi)有http模擬請求,直接讀取服務(wù)器響應速度非???。 S%M/|*M }9A S
2K3` G#J3n&|O Y w.A g
截獲Response的關(guān)鍵便是實(shí)現一個(gè)WrappedResponse,讓服務(wù)器將響應寫(xiě)入我們的WrappedResponse中。這類(lèi)似于一個(gè)代理模式,Servlet 2.x已經(jīng)提供了一個(gè)WrappedResponse類(lèi),我們只需要復寫(xiě)其中的一些關(guān)鍵方法即可。 !c P6I)k y I
Q8@ [ u Q
WrappedResponse實(shí)現了Response接口,它需要一個(gè)Response作為構造函數的參數,事實(shí)上這正是代理模式的應用:WrappedResponse充當了代理角色,它會(huì )將JSP/Servlet容器的某些方法調用進(jìn)行預處理,我們需要實(shí)現自己的方法。 n j T Bp
k
0Q;P(U0h e2h:n
綜上:用Filter實(shí)現HTML緩沖的步驟是:
p D mn h t'L;H
1. 用Filter截獲請求,如/a.jsp?id=123,映射到對應的html文件名為/html/a.jsp$id=123.htm。 8b6h r l!j;T$k z V
2. 查找是否有/html/a.jsp$id=123.htm,如果有,直接forward到此html,結束。
3. 如果沒(méi)有,實(shí)現一個(gè)WrappedResponse,然后調用filterChain(request, wrappedResponse)。
4. 將返回的WrappedResponse寫(xiě)入文件/html/a.jsp$id=123.htm,然后返回響應給用戶(hù)。 Q q,l Z P A P
5. 下一次用戶(hù)發(fā)送相同的請求時(shí),到第2步就結束了。 | J O't ^
*i Q2j m Qu/}"^-^ x
使用這個(gè)方法的好處是不用更改現有的Servlet,JSP頁(yè),限制是,JSP頁(yè)面結果不能與Session相關(guān),需要登陸或用戶(hù)定制的頁(yè)面不能用這種方法緩沖
源代碼:
三、利用Filter和定制Response,把服務(wù)器返回的JSP響應輸出到我們自己的Response中,就可以將響應快速寫(xiě)入Html文件,然后再發(fā)送給客戶(hù)。 p8P!`!N C |
.^ A4w%F*IL [¬|1R
import java.io.*;
import javax.servlet.*; 5\ S o @ l w2L o1W
import javax.servlet.http.*; %^ Q"Ng H*W J Q
import java.util.Calendar; M A&O(~ t C e
S$y u i-v2P ` R+We
public class CacheFilter implements Filter { D i3K/C A e ~4s(~
ServletContext sc; /P1{,Y G0B2X¬Y Q
FilterConfig fc; 8O F/~ u"z [-`
long cacheTimeout = Long.MAX_VALUE; 5h9l9r I l
public void doFilter(ServletRequest req,
ServletResponse res,
FilterChain chain) 1{ P z n [)\
throws IOException, ServletException {
HttpServletRequest request = y/R%A Z d m F l.U s
(HttpServletRequest) req;
HttpServletResponse response =
(HttpServletResponse) res;
// check if was a resource that shouldn't be cached. | N D l!Y em B
String r = sc.getRealPath("");
String path =
fc.getInitParameter(request.getRequestURI());
if (path!= null && path.equals("nocache")) { )b d(e'I7J @
chain.doFilter(request, response);
return; \ _ N i8e
} &a2{ c Y#w u x
path = r+path;
String id = request.getRequestURI() +
request.getQueryString(); 6r | g:i u g"[ Y
File tempDir = (File)sc.getAttribute( p R2P0l J1u$u
"javax.servlet.context.tempdir"); -A%Z C G g5d
P c [;S2x$? | t5Z `5W
// get possible cache
String temp = tempDir.getAbsolutePath();
File file = new File(temp+id); &G/Y1N G*`4y3_
// get current resource
if (path == null) {
path = sc.getRealPath(request.getRequestURI());
}
H K/J f P v H
m @)T
File current = new File(path);
so,Z3[ ~ J2u.D
try {
long now = {:{.i$U Q0} o
Calendar.getInstance().getTimeInMillis(); b Ta#f¬u.f9]
//set timestamp check
if (!file.exists() || (file.exists() &&
current.lastModified() > file.lastModified()) || q Z G H j¬u2_+k
cacheTimeout < now - file.lastModified()) { H ~2X0e*uS
String name = file.getAbsolutePath();
name = @ D2U)V7o }:V U
name.substring(0,name.lastIndexOf("/"));
new File(name).mkdirs();
ByteArrayOutputStream baos =
new ByteArrayOutputStream();
CacheResponseWrapper wrappedResponse = )f7c:s \
o ^ V*a$w S
new CacheResponseWrapper(response, baos); z s y W$u5U+C f
chain.doFilter(req, wrappedResponse); /Y1t [1O(I"a
l x&e X¬u/n {7D$I
FileOutputStream fos = new FileOutputStream(file); m i u { L0M H i
fos.write(baos.toByteArray());
fos.flush();
fos.close();
} 0~#q S L;B
} catch (ServletException e) {
if (!file.exists()) { D @¬? d r L¬x
throw new ServletException(e); f ~1C dl C(R \6q
} ¬q:T p p R-D
} +_ P;? u
L3I.L
catch (IOException e) { R7L0|
A7I7hB
if (!file.exists()) { 6u-i Y L d e A
throw e;
} r F)C I PN
} -X%V i N$w h K.q
FileInputStream fis = new FileInputStream(file);
String mt = sc.getMimeType(request.getRequestURI());
response.setContentType(mt); #l ?-p1e$Y9i R¬|
ServletOutputStream sos = res.getOutputStream();
for (int i = fis.read(); i!= -1; i = fis.read()) { ,f7f Iy4Y8V m.B:T
sos.write((byte)i);
} V A ] ^$Q7I
}
public void init(FilterConfig filterConfig) { h g D3| u k d
this.fc = filterConfig; 7N l!]"X#M
String ct = x¬C m X W `!a I5D7[
fc.getInitParameter("cacheTimeout");
if (ct != null) { Z9d#X7y m"jV
cacheTimeout = 60*1000*Long.parseLong(ct); d
U {6n L*H0s h
} W'W Fz l,?
this.sc = filterConfig.getServletContext();
}
public void destroy() { 6?-K e p4Y j z _ |
this.sc = null;
this.fc = null; 6lo N ] x A
} 0~$T*M$_3e p0m
_,W \
} D:~ F M-}
t!J/b$q4_ Q F s*? u
聯(lián)系客服