网络编程 
首页 > 网络编程 > 浏览文章

动态JSP页生成静态HTML的方法

(编辑:jimmy 日期: 2024/11/26 浏览:3 次 )

本文实例讲述了动态JSP页生成静态HTML的方法。分享给大家供大家参考。具体如下:

具体实现:

利用Filter的过滤功能把*.jsp过滤出来,判断是否存在对应的.html文件,如果不存在对应的.html文件,则将其内容读出写入指定的.html文件,再跳转到对应的.html即可。如果存在对应的.html文件,则直接跳转到对应的.html即可。
 
代码:

JspFilter.java:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class JspFilter implements Filter {
  public void destroy() {
    // TODO 自动生成方法存根
  }
  public void doFilter(ServletRequest request, ServletResponse response,
      FilterChain chain) throws IOException, ServletException {
    HttpServletRequest hreq = (HttpServletRequest) request;
    HttpServletResponse hresp = (HttpServletResponse) response;
    String name = hreq.getRequestURL().substring(
          hreq.getRequestURL().lastIndexOf("/") + 1,
          hreq.getRequestURL().lastIndexOf("."));
    if (hreq.getRequestURL().indexOf(".jsp") != -1 && (null == hreq.getParameter("type") || hreq.getParameter("type").equals(""))) {
        hresp.sendRedirect(hreq.getContextPath()+"/conversion"+hreq.getRequestURL());
        return ;
    }
    chain.doFilter(request, response);
  }
  public void init(FilterConfig arg0) throws ServletException {
    // TODO 自动生成方法存根
  }
}

ConversionServlet.java:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ConversionServlet extends HttpServlet {
  public ConversionServlet () {
    super();
  }
  public void destroy() {
    super.destroy(); // Just puts "destroy" string in log
    // Put your code here
  }
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    //----获取请求的URL
    String reqTarget = request.getParameter("name");
    //----指定对应JSP的HTML名称
    String name = reqTarget.substring(reqTarget.lastIndexOf("/") + 1,reqTarget.lastIndexOf("."));
    //---判断是否存在对应的HTML文件
    File file = new File(request.getRealPath("/") + name + ".html");
    if (!file.exists()) {  //--------如果不存在对应的HTML文件
      try {
        file.createNewFile();  //--------创建HTML文件
        //-------将JSP的内容写入对应的HTML文件内
        InputStream in;
        StringBuffer sb = new StringBuffer("");
      //----注意这里,不能直接访问请求的URL,如果直接访问的话,会陷入死循环
        URL url = new java.net.URL(reqTarget.toString()+"");
        HttpURLConnection connection = (HttpURLConnection) url
            .openConnection();
        connection.setRequestProperty("User-Agent", "Mozilla/4.0");
        connection.connect();
        in = connection.getInputStream();
        java.io.BufferedReader breader = new BufferedReader(
            new InputStreamReader(in, "GBK"));
        String currentLine;
        FileOutputStream fos = new FileOutputStream(file);
        while ((currentLine = breader.readLine()) != null) {
          sb.append(currentLine);
          fos.write(currentLine.getBytes());
        }
        if (null != breader)
          breader.close();
        if (null != fos)
          fos.close();
        //---------------转到与JSP对应的HTML页
        response.sendRedirect(request.getContextPath()+"/"+name + ".html");
      } catch (Exception e) {
        e.printStackTrace();
      }
    }else{
      //----------如果存在指定的HTML,直接跳转到指定的HTML页
      response.sendRedirect(request.getContextPath()+"/"+name + ".html");
    }
  }
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    doGet(request, response);
  }
  public void init() throws ServletException {
    // Put your code here
  }
}

WEB.XML配置:

<"1.0" encoding="UTF-8""2.4"
  xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <filter>
  <filter-name>jspfilter</filter-name>
  <filter-class>com.beanutil.JspFilter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>jspfilter</filter-name>
  <url-pattern>*.jsp</url-pattern>
 </filter-mapping>
 <servlet>
  <servlet-name>conversion</servlet-name>
  <servlet-class>com.beanutil.ConversionServlet </servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>conversion</servlet-name>
  <url-pattern>/conversion</url-pattern>
 </servlet-mapping>
</web-app>

希望本文所述对大家的JSP程序设计有所帮助。

上一篇:Java Web开发之MD5加密用法分析
下一篇:JSP+EXt2.0实现分页的方法
一句话新闻
一文看懂荣耀MagicBook Pro 16
荣耀猎人回归!七大亮点看懂不只是轻薄本,更是游戏本的MagicBook Pro 16.
人们对于笔记本电脑有一个固有印象:要么轻薄但性能一般,要么性能强劲但笨重臃肿。然而,今年荣耀新推出的MagicBook Pro 16刷新了人们的认知——发布会上,荣耀宣布猎人游戏本正式回归,称其继承了荣耀 HUNTER 基因,并自信地为其打出“轻薄本,更是游戏本”的口号。
众所周知,寻求轻薄本的用户普遍更看重便携性、外观造型、静谧性和打字办公等用机体验,而寻求游戏本的用户则普遍更看重硬件配置、性能释放等硬核指标。把两个看似难以相干的产品融合到一起,我们不禁对它产生了强烈的好奇:作为代表荣耀猎人游戏本的跨界新物种,它究竟做了哪些平衡以兼顾不同人群的各类需求呢?
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 网站地图 SiteMap