JSP頁面靜態化

來源:互聯網
上載者:User

標籤:blog   http   java   使用   檔案   os   

package com.pageStatic; import java.io.BufferedReader;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLConnection; public class Test { public static void convert2Html(String sUrl, String charset, String sSavePath, String sHtmlFile) throws IOException {       int HttpResult;       URL url = new URL(sUrl);       URLConnection urlconn = url.openConnection();       System.out.println(sSavePath);       // 抽象類別 URLConnection       // 是所有類的超類,它代表應用程式和 URL       // 之間的通訊連結,通過在 URL 上調用       // openConnection 方法建立連線物件       urlconn.connect(); // 使用 connect 方法建立到遠程對象的實際串連       HttpURLConnection httpconn = (HttpURLConnection) urlconn;       // 每個       // HttpURLConnection       // 執行個體都可用於產生單個請求,       // 但是其他執行個體可以透明地共用串連到       // HTTP 伺服器的基礎網路        HttpResult = httpconn. getResponseCode();       // getResponseCode可以從 HTTP       // 響應訊息擷取狀態代碼       if (HttpResult != HttpURLConnection.HTTP_OK) {         } else {       InputStreamReader isr = new InputStreamReader(httpconn. getInputStream(),       charset);       BufferedReader in = new BufferedReader(isr);       String inputLine;       if (!sSavePath.endsWith("/")) {          sSavePath += "/";       }      FileOutputStream fout = new FileOutputStream(sSavePath + sHtmlFile);     while ((inputLine = in.readLine()) != null) {        System.out.println(inputLine);         fout.write((inputLine+"/n").getBytes());     }       in.close();      fout.close();   } } public static void main(String[] args) throws IOException {          Test ru = new Test();          String filePath =ru. getClass(). getResource("."). getPath().toString(); // 取得項目根目錄          filePath = filePath.substring(1, filePath.indexOf("WEB-INF"));          convert2Html("http://localhost:8088/huajiao/index1.do", "UTF-8", filePath + "/","index.html"); }}

 

參數sUrl可以是.do 也可以是jsp

 

把web.xml裡的配置

<welcome-file-list>
  <welcome-file>/sHtmlFile(產生之後的HTML檔案,在此就是index.html)</welcome-file>
 </welcome-file-list>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.