最近在做一個項目,其中一部分的訪問頻率較高但內(nèi)容變動較小。所以使用網(wǎng)站HTML靜態(tài)化方案來優(yōu)化訪問速度
當(dāng)一個Servlet資源請求到達WEB服務(wù)器之后我們會填充指定的JSP頁面來響應(yīng)請求:
HTTP請求---Web服務(wù)器---Servlet--業(yè)務(wù)邏輯處理--訪問數(shù)據(jù)--填充JSP--響應(yīng)請求
HTML靜態(tài)化之后:
HTTP請求---Web服務(wù)器---Servlet--HTML--響應(yīng)請求
嘿嘿,是不是很爽?省去了業(yè)務(wù)邏輯處理和數(shù)據(jù)抓取直接響應(yīng)。
Servlet:
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- if(request.getParameter("chapterId") != null){
- String chapterFileName = "bookChapterRead_"+request.getParameter("chapterId")+".html";
- String chapterFilePath = getServletContext().getRealPath("/") + chapterFileName;
- File chapterFile = new File(chapterFilePath);
- if(chapterFile.exists()){response.sendRedirect(chapterFileName);return;}
- INovelChapterBiz novelChapterBiz = new NovelChapterBizImpl();
- NovelChapter novelChapter = novelChapterBiz.searchNovelChapterById(Integer.parseInt(request.getParameter("chapterId")));
- int lastPageId = novelChapterBiz.searchLastCHapterId(novelChapter.getNovelId().getId(), novelChapter.getId());
- int nextPageId = novelChapterBiz.searchNextChapterId(novelChapter.getNovelId().getId(), novelChapter.getId());
- request.setAttribute("novelChapter", novelChapter);
- request.setAttribute("lastPageId", lastPageId);
- request.setAttribute("nextPageId", nextPageId);
- new CreateStaticHTMLPage().createStaticHTMLPage(request, response, getServletContext(),
- chapterFileName, chapterFilePath, "/bookRead.jsp");
- }
- }
生成HTML靜態(tài)頁面的類:
- package com.jb.y2t034.thefifth.web.servlet;
- import java.io.ByteArrayOutputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStreamWriter;
- import java.io.PrintWriter;
- import javax.servlet.RequestDispatcher;
- import javax.servlet.ServletContext;
- import javax.servlet.ServletException;
- import javax.servlet.ServletOutputStream;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpServletResponseWrapper;
-
-
-
-
-
-
-
-
- public class CreateStaticHTMLPage {
-
-
-
-
-
-
-
-
-
-
-
- public void createStaticHTMLPage(HttpServletRequest request, HttpServletResponse response,ServletContext servletContext,String fileName,String fileFullPath,String jspPath) throws ServletException, IOException{
- response.setContentType("text/html;charset=gb2312");
- RequestDispatcher rd = servletContext.getRequestDispatcher(jspPath);
- final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
- final ServletOutputStream servletOuputStream = new ServletOutputStream(){
- public void write(byte[] b, int off,int len){
- byteArrayOutputStream.write(b, off, len);
- }
- public void write(int b){
- byteArrayOutputStream.write(b);
- }
- };
- final PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(byteArrayOutputStream));
- HttpServletResponse httpServletResponse = new HttpServletResponseWrapper(response){
- public ServletOutputStream getOutputStream(){
- return servletOuputStream;
- }
- public PrintWriter getWriter(){
- return printWriter;
- }
- };
- rd.include(request, httpServletResponse);
- printWriter.flush();
- FileOutputStream fileOutputStream = new FileOutputStream(fileFullPath);
- byteArrayOutputStream.writeTo(fileOutputStream);
- fileOutputStream.close();
- response.sendRedirect(fileName);
- }
- }
引用:http://blog.csdn.net/zjwilove4/archive/2009/10/18/4695518.aspx