jsp文件转HTML文件

原理:实际上就是把JSP编译后的网页的源码 写成一个HTML

  1.  toHtml.java文件
package com.peter.test;
 
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.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
 
/*******************************************************************************
 *
 */
publicclass toHtml extends HttpServlet {
    publicvoid service(HttpServletRequest request, HttpServletResponse response)
           throws ServletException, IOException {
       String url = request.getParameter("urls") != null ? request
              .getParameter("urls") : "";
       // url是要生成htmjsp页面,只接受后缀名为三位数的文件
       String name = "";
       response.setContentType("text/html;charset=gb2312");
       ServletContext sc = getServletContext();
       // System.out.println("request.getRealPath: "+request.getContextPath());
       name = getServletContext().getRealPath("/")
              + url.substring(0, url.length() - 4) + ".htm";
       // System.out.println(request.getContextPath());
       // System.out.println(getServletContext().getRealPath("/"));
       /*
        * 生成htm位置在%服务器的根目录%/工程名/原文件路径+原文件名.htm
        * 例如:d:/tomcat5.0/webapp/JspToHtml/Test.htm ,
        * 注意这里是在根目录生成,你可以生成后放到你想放的文件夹里, 保证其它东西如图片的路径正确 name 就是生成的HTML文件;
        * 生成的新页面与原来的页面在同一个路径,同一文件名,只是后缀改为Htm
        */
       RequestDispatcher rd = sc.getRequestDispatcher(url);
       final ByteArrayOutputStream os = new ByteArrayOutputStream();
       final ServletOutputStream stream = new ServletOutputStream() {
           publicvoid write(byte[] data, int offset, int length) {
              os.write(data, offset, length);
           }
 
           publicvoid write(int b) throws IOException {
              os.write(b);
           }
       };
       final PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));
       HttpServletResponse rep = new HttpServletResponseWrapper(response) {
           public ServletOutputStream getOutputStream() {
              return stream;
           }
 
           public PrintWriter getWriter() {
              return pw;
           }
       };
       rd.include(request, rep);
       pw.flush();
       FileOutputStream fos = new FileOutputStream(name);
       // jsp输出的内容写到xxx.htm
       os.writeTo(fos);
       fos.close();
       PrintWriter out = response.getWriter();
       out.print("<p align=center><font size=3 color=red>首页已经成功生成!</font></p>");
    }
}

           2.接着添加servlet到web.xml

     <servlet>
      <servlet-name>tohtm</servlet-name>
      <servlet-class>com.peter.test.toHtml</servlet-class>
     </servlet>
     <servlet-mapping>
      <servlet-name>tohtm</servlet-name>
      <url-pattern>/web/tohtm</url-pattern>
     </servlet-mapping>

         3.最后在jsp里写一个链接,index.jsp为要转换为HTML的index.jsp

  <a href="web/tohtm?urls=/index.jsp">生成静态页</a>
   4.在根目录下新建一个index.html
   5.生成html文件

         

 

 

展开阅读全文

没有更多推荐了,返回首页