主頁(yè) > 知識(shí)庫(kù) > 動(dòng)態(tài)JSP頁(yè)生成靜態(tài)HTML的方法

動(dòng)態(tài)JSP頁(yè)生成靜態(tài)HTML的方法

熱門(mén)標(biāo)簽:電銷(xiāo)機(jī)器人是有一些什么技術(shù) 杭州ai語(yǔ)音電銷(xiāo)機(jī)器人功能 高德地圖標(biāo)注樣式 地圖標(biāo)注線(xiàn)上教程 商洛電銷(xiāo) 北票市地圖標(biāo)注 電銷(xiāo)機(jī)器人好賣(mài)么 杭州語(yǔ)音電銷(xiāo)機(jī)器人軟件 四川保險(xiǎn)智能外呼系統(tǒng)商家

本文實(shí)例講述了動(dòng)態(tài)JSP頁(yè)生成靜態(tài)HTML的方法。分享給大家供大家參考。具體如下:

具體實(shí)現(xiàn):

利用Filter的過(guò)濾功能把*.jsp過(guò)濾出來(lái),判斷是否存在對(duì)應(yīng)的.html文件,如果不存在對(duì)應(yīng)的.html文件,則將其內(nèi)容讀出寫(xiě)入指定的.html文件,再跳轉(zhuǎn)到對(duì)應(yīng)的.html即可。如果存在對(duì)應(yīng)的.html文件,則直接跳轉(zhuǎn)到對(duì)應(yīng)的.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 自動(dòng)生成方法存根
  }
  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?name="+hreq.getRequestURL());
        return ;
    }
    chain.doFilter(request, response);
  }
  public void init(FilterConfig arg0) throws ServletException {
    // TODO 自動(dòng)生成方法存根
  }
}

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();
    //----獲取請(qǐng)求的URL
    String reqTarget = request.getParameter("name");
    //----指定對(duì)應(yīng)JSP的HTML名稱(chēng)
    String name = reqTarget.substring(reqTarget.lastIndexOf("/") + 1,reqTarget.lastIndexOf("."));
    //---判斷是否存在對(duì)應(yīng)的HTML文件
    File file = new File(request.getRealPath("/") + name + ".html");
    if (!file.exists()) {  //--------如果不存在對(duì)應(yīng)的HTML文件
      try {
        file.createNewFile();  //--------創(chuàng)建HTML文件
        //-------將JSP的內(nèi)容寫(xiě)入對(duì)應(yīng)的HTML文件內(nèi)
        InputStream in;
        StringBuffer sb = new StringBuffer("");
      //----注意這里,不能直接訪問(wèn)請(qǐng)求的URL,如果直接訪問(wèn)的話(huà),會(huì)陷入死循環(huán)
        URL url = new java.net.URL(reqTarget.toString()+"?type=11");
        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();
        //---------------轉(zhuǎn)到與JSP對(duì)應(yīng)的HTML頁(yè)
        response.sendRedirect(request.getContextPath()+"/"+name + ".html");
      } catch (Exception e) {
        e.printStackTrace();
      }
    }else{
      //----------如果存在指定的HTML,直接跳轉(zhuǎn)到指定的HTML頁(yè)
      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配置:

?xml version="1.0" encoding="UTF-8"?>
web-app version="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>

希望本文所述對(duì)大家的JSP程序設(shè)計(jì)有所幫助。

您可能感興趣的文章:
  • jsp實(shí)現(xiàn)將動(dòng)態(tài)網(wǎng)頁(yè)轉(zhuǎn)換成靜態(tài)頁(yè)面的方法
  • jsp中include指令靜態(tài)導(dǎo)入和動(dòng)態(tài)導(dǎo)入的區(qū)別詳解
  • JSP中動(dòng)態(tài)include與靜態(tài)include的區(qū)別介紹
  • JSP技術(shù)實(shí)現(xiàn)動(dòng)態(tài)頁(yè)面到靜態(tài)頁(yè)面的方法

標(biāo)簽:青島 丹東 云浮 紅河 江西 西藏 貴州 宿州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《動(dòng)態(tài)JSP頁(yè)生成靜態(tài)HTML的方法》,本文關(guān)鍵詞  動(dòng)態(tài),JSP,頁(yè),生成,靜態(tài),HTML,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《動(dòng)態(tài)JSP頁(yè)生成靜態(tài)HTML的方法》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于動(dòng)態(tài)JSP頁(yè)生成靜態(tài)HTML的方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章