静态页面的html用request,网站HTML静态化解决方案

最近在做一个项目,其中一部分的访问频率较高但内容变动较小。所以使用网站HTML静态化方案来优化访问速度

当一个Servlet资源请求到达WEB服务器之后我们会填充指定的JSP页面来响应请求:

HTTP请求---Web服务器---Servlet--业务逻辑处理--访问数据--填充JSP--响应请求

HTML静态化之后:

HTTP请求---Web服务器---Servlet--HTML--响应请求

嘿嘿,是不是很爽?省去了业务逻辑处理和数据抓取直接响应。

Servlet:

publicvoiddoGet(HttpServletRequest request, HttpServletResponse response)

throwsServletException, IOException {

if(request.getParameter("chapterId") !=null){

String chapterFileName ="bookChapterRead_"+request.getParameter("chapterId")+".html";

String chapterFilePath = getServletContext().getRealPath("/") + chapterFileName;

File chapterFile =newFile(chapterFilePath);

if(chapterFile.exists()){response.sendRedirect(chapterFileName);return;}//如果有这个文件就告诉浏览器转向

INovelChapterBiz novelChapterBiz =newNovelChapterBizImpl();

NovelChapter novelChapter = novelChapterBiz.searchNovelChapterById(Integer.parseInt(request.getParameter("chapterId")));//章节信息

intlastPageId = novelChapterBiz.searchLastCHapterId(novelChapter.getNovelId().getId(), novelChapter.getId());

intnextPageId = novelChapterBiz.searchNextChapterId(novelChapter.getNovelId().getId(), novelChapter.getId());

request.setAttribute("novelChapter", novelChapter);

request.setAttribute("lastPageId", lastPageId);

request.setAttribute("nextPageId", nextPageId);

newCreateStaticHTMLPage().createStaticHTMLPage(request, response, getServletContext(),

chapterFileName, chapterFilePath,"/bookRead.jsp");

}

}

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静态页面的类:

packagecom.jb.y2t034.thefifth.web.servlet;

importjava.io.ByteArrayOutputStream;

importjava.io.FileOutputStream;

importjava.io.IOException;

importjava.io.OutputStreamWriter;

importjava.io.PrintWriter;

importjavax.servlet.RequestDispatcher;

importjavax.servlet.ServletContext;

importjavax.servlet.ServletException;

importjavax.servlet.ServletOutputStream;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

importjavax.servlet.http.HttpServletResponseWrapper;

/**

* 创建HTML静态页面

* 功能:创建HTML静态页面

* 时间:2009年1011日

* 地点:home

* @author mavk

*

*/

publicclassCreateStaticHTMLPage {

/**

* 生成静态HTML页面的方法

* @param request 请求对象

* @param response 响应对象

* @param servletContext Servlet上下文

* @param fileName 文件名称

* @param fileFullPath 文件完整路径

* @param jspPath 需要生成静态文件的JSP路径(相对即可)

* @throws IOException

* @throws ServletException

*/

publicvoidcreateStaticHTMLPage(HttpServletRequest request, HttpServletResponse response,ServletContext servletContext,String fileName,String fileFullPath,String jspPath)throwsServletException, IOException{

response.setContentType("text/html;charset=gb2312");//设置HTML结果流编码(即HTML文件编码)

RequestDispatcher rd = servletContext.getRequestDispatcher(jspPath);//得到JSP资源

finalByteArrayOutputStream byteArrayOutputStream =newByteArrayOutputStream();//用于从ServletOutputStream中接收资源

finalServletOutputStream servletOuputStream =newServletOutputStream(){//用于从HttpServletResponse中接收资源

publicvoidwrite(byte[] b,intoff,intlen){

byteArrayOutputStream.write(b, off, len);

}

publicvoidwrite(intb){

byteArrayOutputStream.write(b);

}

};

finalPrintWriter printWriter =newPrintWriter(newOutputStreamWriter(byteArrayOutputStream));//把转换字节流转换成字符流

HttpServletResponse httpServletResponse =newHttpServletResponseWrapper(response){//用于从response获取结果流资源(重写了两个方法)

publicServletOutputStream getOutputStream(){

returnservletOuputStream;

}

publicPrintWriter getWriter(){

returnprintWriter;

}

};

rd.include(request, httpServletResponse);//发送结果流

printWriter.flush();//刷新缓冲区,把缓冲区的数据输出

FileOutputStream fileOutputStream =newFileOutputStream(fileFullPath);

byteArrayOutputStream.writeTo(fileOutputStream);//把byteArrayOuputStream中的资源全部写入到fileOuputStream中

fileOutputStream.close();//关闭输出流,并释放相关资源

response.sendRedirect(fileName);//发送指定文件流到客户端

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值