1. package com.htmlstatic; 2. 3. import java.io.ByteArrayOutputStream; 4. import java.io.File; 5. import java.io.FileNotFoundException; 6. import java.io.FileOutputStream; 7. import java.io.IOException; 8. import java.io.OutputStreamWriter; 9. import java.io.PrintWriter; 10. import java.util.ArrayList; 11. import java.util.List; 12. 13. import javax.servlet.RequestDispatcher; 14. import javax.servlet.ServletContext; 15. import javax.servlet.ServletException; 16. import javax.servlet.ServletOutputStream; 17. import javax.servlet.http.HttpServlet; 18. import javax.servlet.http.HttpServletRequest; 19. import javax.servlet.http.HttpServletResponse; 20. import javax.servlet.http.HttpServletResponseWrapper; 21. 22. public class ToHtml extends HttpServlet { 23. private static final String CONTENT_TYPE = "text/html; charset=GBK"; //$NON-NLS-1$ 24. 25. private static ServletContext sc = null; 26. 27. private HttpServletRequest request = null; 28. private HttpServletResponse response = null; 29. private static String staticHtmlRefreshTime = "1";//刷新间隔时间 30. private static String path = "D://tomcat5.5//webapps//diesel_gdc";//文件生成路径 31. private static String url = "/index.jsp";//需要静态化的页面 32. 33. 34. // Initialize global variables 35. public void init() throws ServletException { 36. } 37. 38. // Process the HTTP Get request 39. public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 40. response.setContentType(CONTENT_TYPE); 41. service(request, response); 42. } 43. 44. // Process the HTTP Post request 45. public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 46. doGet(request, response); 47. } 48. 49. public void destroy() { 50. } 51. 52. public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 53. response.setContentType(CONTENT_TYPE); 54. long SPACING = 0; 55. if (staticHtmlRefreshTime == null) { 56. SPACING = 1000 * 60 * 10; 57. } else { 58. SPACING = 1000 * 60 * Long.parseLong(staticHtmlRefreshTime); 59. } 60. 61. sc = getServletContext(); 62. this.request = request; 63. this.response = response; 64. 65. String[] urls = url.split(","); 66. 67. List names = new ArrayList(); 68. for (int i = 0; i < urls.length; i++) { 69. String tmp = urls[i].replaceAll(".jsp", ".html"); 70. names.add(tmp); 71. } 72. while (true) { 73. System.err.println("静态化开始"); 74. myService(urls, names); 75. System.err.println("静态化结束"); 76. try { 77. Thread.currentThread().sleep(SPACING); 78. } catch (Exception e) { 79. e.printStackTrace(); 80. } 81. } 82. } 83. 84. public synchronized void myService(String[] urls, List names) { 85. 86. String name = ""; 87. for (int i = 0; i < urls.length; i++) { 88. FileOutputStream fos = null; 89. try { 90. name = path + names.get(i).toString().replaceAll(".html", ".html"); 91. RequestDispatcher rd = sc.getRequestDispatcher("/" + urls[i]); 92. final ByteArrayOutputStream os = new ByteArrayOutputStream(); 93. final ServletOutputStream stream = new ServletOutputStream() { 94. public void write(byte[] data, int offset, int length) { 95. os.write(data, offset, length); 96. } 97. 98. public void write(int b) throws IOException { 99. os.write(b); 100. } 101. }; 102. final PrintWriter pw = new PrintWriter(new OutputStreamWriter(os)); 103. HttpServletResponse rep = new HttpServletResponseWrapper(response) { 104. public ServletOutputStream getOutputStream() { 105. return stream; 106. } 107. 108. public PrintWriter getWriter() { 109. return pw; 110. } 111. }; 112. 113. rd.include(request, rep); 114. pw.flush(); 115. System.out.println("name=="+name); 116. 117. fos = new FileOutputStream(name); 118. // 把jsp输出的内容写到xxx.htm 119. os.writeTo(fos); 120. } catch (FileNotFoundException e) { 121. e.printStackTrace(); 122. } catch (ServletException e) { 123. e.printStackTrace(); 124. } catch (IOException e) { 125. e.printStackTrace(); 126. } finally { 127. try { 128. fos.close(); 129. } catch (Exception e) { 130. } 131. } 132. 133. } 134. } 135. 136. private void writeFile(String fileName, String content) { 137. OutputStreamWriter writer = null; 138. try { 139. File f = new File(path + "//" + fileName); 140. if (!f.exists()) { 141. f.createNewFile(); 142. } 143. writer = new OutputStreamWriter(new FileOutputStream(f), "GBK"); 144. writer.write(content); 145. writer.flush(); 146. } catch (FileNotFoundException e) { 147. e.printStackTrace(); 148. } catch (IOException e) { 149. e.printStackTrace(); 150. } finally { 151. try { 152. writer.close(); 153. } catch (IOException e) { 154. e.printStackTrace(); 155. } 156. } 157. } 158. } # <servlet> # <servlet-name>tohtml</servlet-name> # <servlet-class>com.htmlstatic.ToHtml</servlet-class> # </servlet> # <servlet-mapping> # <servlet-name>tohtml</servlet-name> # <url-pattern>*.tohtml</url-pattern> # </servlet-mapping>