一、实现原因
1.网站访问量过大,导致服务器压力加大以及数据库数据交换频繁。生成静态页面提供访问以缓解压力。
2.静态页面是动态页面的备份,若动态页面出现异常,静态页面可以暂时替代。
二、使用场合
当某个页面访问量很大,且数据不经常变动适合转换为html存储。如网站首页,新闻文章页等
三、实现方法
实现方法有很多,有框架自动生成,有重写输出方法,有用缓存机制从而无需生成静态页面等,此处介绍两种方法。
1.使用servlet发送请求读取jsp动态数据,将数据写入html文件并保存。
a.首先需要一个servlet类,service方法中是生成静态页面的主要代码。
1 /** 2 ?* @file_name 文件名及文件之后的参数 3 ?* @path 文件所在的路径.相对于根目录而言的. 4 ?* @realName 文件要保存的名字 5 ?* @realPath 文件要保存的真实路径, 默认与文件所在的目录相同。 6 ?*/ 7 public class JspToHtml extends HttpServlet { 8 ?9 ????public void service(HttpServletRequest request, HttpServletResponse response)10 ????????????throws ServletException, IOException {11 12 ????????HttpSession session = request.getSession();13 ????????String url = "";14 ????????String name = "";15 ????????ServletContext sc = getServletContext();16 17 ????????String file_name = request.getParameter("file_name");// 你要访问的jsp文件,如news.jsf。18 ????????// file_name如:fileDetail.jsf?fileId=56.要是有参数, 只有一个参数。并且以参数名作为文件名。19 20 ????????String realName = request.getParameter("realName");// 要保存的文件名。如aaa;注意可以没有这个参数。21 22 ????????String path = request.getParameter("path");// 你要访问的jsp文件路径。如news。注意可以没有这个参数。23 24 ????????String realPath = request.getParameter("realPath");// 你要保存的文件路径,如htmlNews.注意可以没有这个参数。25 ????????// 下面确定要保存的文件名字。26 ????????if (realName == null || realName == "") {27 ????????????int a = 0;28 ????????????a = file_name.indexOf("=") + 1;29 ????????????realName = file_name.substring(a);30 ????????????if (realName.indexOf(".") > 0) {31 ????????????????realName = file_name.substring(0, file_name.indexOf("."));32 ????????????}33 ????????}34 ????????// 下面构造要访问的页面。35 ????????if (path == null || path.equals("")) {36 ????????????url = "/" + file_name;// 这是你要生成HTML的jsp文件,如37 ????????} else {38 ????????????url = "/" + path + "/" + file_name;// 这是你要生成HTML的jsp文件,如39 ????????}40 ????????// 下面构造要保存的文件名,及路径。41 ????????// 1、如果有realPath,则保存在realPath下。42 ????????// 2、如果有path则保存在path下。43 ????????// 3、否则,保存在根目录下。44 ????????if (realPath == null || realPath.equals("")) {45 ????????????if (path == null || path.equals("")) {46 ????????????????// 这是生成的html文件名,如index.htm.说明:47 ????????????????// ConfConstants.CONTEXT_PATH为你的上下文路径。48 ????????????????name = session.getServletContext().getRealPath("") + "\\" + realName + ".html";49 ????????????} else {50 ????????????????name = session.getServletContext().getRealPath("") + "\\" + path + "\\" + realName51 ????????????????????????+ ".html";// 这是生成的html文件名,如index.html52 ????????????}53 ????????} else {54 ????????????name = session.getServletContext().getRealPath("") + "\\" + realPath + "\\" + realName55 ????????????????????+ ".html";// 这是生成的html文件名,如index.html56 ????????}57 ????????File file = new File(name.substring(0, name.length() - (realName.length() + 5)));58 ????????if (!file.exists()) {59 ????????????file.mkdir();60 ????????}61 ????????// 访问请求的页面,并生成指定的文件。62 ????????RequestDispatcher rd = sc.getRequestDispatcher(url);63 64 ????????final ByteArrayOutputStream os = new ByteArrayOutputStream();65 66 ????????final ServletOutputStream stream = new ServletOutputStream() {67 ????????????public void write(byte[] data, int offset, int length) {68 ????????????????os.write(data, offset, length);69 ????????????}70 71 ????????????public void write(int b) throws IOException {72 ????????????????os.write(b);73 ????????????}74 ????????};75 76 ????????//此处需要转码,防止中文字符乱码77 ????????final PrintWriter pw = new PrintWriter(new OutputStreamWriter(os, "UTF-8"));78 ????????79 ???????80 ????????HttpServletResponse rep = new HttpServletResponseWrapper(response) {81 ????????????public ServletOutputStream getOutputStream() {82 ????????????????return stream;83 ????????????}84 85 ????????????public PrintWriter getWriter() {86 ????????????????return pw;87 ????????????}88 ????????};89 ????????rep.setCharacterEncoding("gbk");// response的编码为gbk防乱码90 ????????rd.include(request, rep);91 ????????pw.flush();92 ????????FileOutputStream fos = new FileOutputStream(name); // 把jsp输出的内容写到xxx.html93 94 ????????os.writeTo(fos);95 ????????fos.close();96 ????}97 }
b.调用servlet的方法,这个方法即是后台发送请求访问servlet的方法。
1 // 这个方法适当重载,就可以省去一些参数传递。 2 ????public static void CallOnePage(String basepath, String fileName, String path, String realName, 3 ????????????String realPath) { 4 ????????try { 5 ????????????String str = basepath + "toHtmlPath?file_name=" + fileName + "&path=" + path 6 ????????????????????+ "&realName=" + realName + "&realPath=" + realPath; 7 ????????????System.out.println(str); 8 ????????????int httpResult; // 请求返回码 9 10 ????????????URL url = new URL(str); // URL发送指定连接请求11 ????????????URLConnection connection = url.openConnection();12 ????????????connection.connect();13 ????????????HttpURLConnection httpURLConnection = (HttpURLConnection) connection;14 ????????????httpResult = httpURLConnection.getResponseCode();15 16 ????????????if (httpResult != HttpURLConnection.HTTP_OK) { // 返回码为200则连接成功17 ????????????????System.out.println("没有连接成功!");18 ????????????} else {19 ????????????????System.out.println("连接成功了!!!!!");20 ????????????}21 ????????} catch (Exception e) {22 ????????????e.printStackTrace();23 ????????}24 ????}
c.需要在web.xml里面配置servlet
<servlet> ???????<servlet-name>jspToHtml</servlet-name> ???????<servlet-class>com.adam.util.JspToHtml</servlet-class> ???</servlet> ???<servlet-mapping> ???????<servlet-name>jspToHtml</servlet-name> ???????<url-pattern>/jspToHtml</url-pattern> ???</servlet-mapping>
此时便可以通过第b步的方法来生成html页面了,如
CallOnePage("http://localhost:8080/adam/","test/testHtml.do?method=save&id=1100&subid=1151&tohtml=1", "", "index", "htmls");
生成index.html文件存放在项目目录的htmls文件夹下。
2.使用httpclient生成静态html
a.首先导入httpclient的jar文件(commons-httpclient-3.0.1 解压后取出jar文件导入)
b.实现主生成方法
1 /** 2 ?????* @basePath url host信息 3 ?????* @target 需要转换的链接 4 ?????* @path 文件要保存的真实路径 5 ?????* @name 文件要保存的名字 6 ?????* @extension 文件要保存的扩展名.html/.htm 7 ?????*/ 8 ????public static boolean staticHtml(String basePath, String target, String path, String name, 9 ????????????String extension) {10 ????????boolean result = true;11 ????????HttpClient client = new HttpClient();12 ????????GetMethod getMethod = new GetMethod(basePath + "/" + target);13 14 ????????System.err.println(path);15 ????????try {16 ????????????client.executeMethod(getMethod);17 18 ????????????File filePath = new File(path);19 ????????????if (!filePath.exists()) {20 ????????????????filePath.mkdirs();21 ????????????}22 23 ????????????File file = new File(path + name + extension);24 25 ????????????// FileWriter writer = new FileWriter(file);26 ????????????// writer.write(getMethod.getResponseBodyAsString());27 ????????????// writer.flush();28 29 ????????????//FileWriter 无法指定编码格式,此处替代使用防止存储的文件乱码30 ????????????Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),31 ????????????????????"UTF-8"));32 ????????????33 ????????????out.write(getMethod.getResponseBodyAsString());34 ????????????out.flush();35 ????????????out.close();36 ????????} catch (Exception e) {37 ????????????e.printStackTrace();38 ????????????result = false;39 ????????}40 ????????return result;41 ????}
此时便可以通过此方法来生成html页面了,如
staticHtml("http://localhost:8080/btbasic", "main/main.do?method=main", path, "index",".html");
便可以生成index.html放在指定的path下了。
jsp实现html页面静态化
原文地址:http://www.cnblogs.com/adamJin/p/7717398.html