精易论坛

标题: Jsp生成静态Html文件的方法 [打印本页]

作者: yueying    时间: 2017-4-26 07:28
标题: Jsp生成静态Html文件的方法

为了减轻服务器压力,将原来的文章管理系统由JSP文件的从数据库中取数据显示改为由jsp生成静态html文件后直接访问html文件。下面是一个简单的示例

buildhtml.jsp


  1. <%[url=home.php?mod=space&uid=398972]@[/url] page contentType="text/html; charset=gb2312" import="java.util.*,java.io.*"%>

  2. <%
  3. try{
  4. String title="生成静态html文件";
  5. String content="aaaaaaaaa";
  6. String editer="hpsoft";
  7. String filePath = "";
  8. filePath = request.getRealPath("/")+"template.htm";
  9. out.print(filePath);
  10. String templateContent="";
  11. FileInputStream fileinputstream = new FileInputStream(filePath);//读取模块文件
  12. int lenght = fileinputstream.available();
  13. byte bytes[] = new byte[lenght];
  14. fileinputstream.read(bytes);
  15. fileinputstream.close();
  16. templateContent = new String(bytes);
  17. out.print(templateContent);
  18. templateContent=templateContent.replaceAll("###title###",title);
  19. templateContent=templateContent.replaceAll("###content###",content);
  20. templateContent=templateContent.replaceAll("###author###",editer);
  21. //替换掉模块中相应的方法
  22. out.print(templateContent);
  23. // 根据时间得文件名
  24. Calendar calendar = Calendar.getInstance();
  25. String fileame = String.valueOf(calendar.getTimeInMillis()) +".html";
  26. fileame = request.getRealPath("/")+fileame;//生成的html文件保存路径
  27. FileOutputStream fileoutputstream = new FileOutputStream(fileame);//建立文件输出流
  28. byte tag_bytes[] = templateContent.getBytes();
  29. fileoutputstream.write(tag_bytes);
  30. fileoutputstream.close();
  31. }
  32. catch(Exception e){
  33. out.print(e.toString());
  34. }
  35. %>
复制代码







欢迎光临 精易论坛 (https://125.confly.eu.org/) Powered by Discuz! X3.4