jsp產生靜態頁面的方法

來源:互聯網
上載者:User

import java.io.ByteArrayOutputStream;<br />import java.io.FileOutputStream;<br />import java.io.IOException;<br />import java.io.OutputStreamWriter;<br />import java.io.PrintWriter;<br />import javax.servlet.RequestDispatcher;<br />import javax.servlet.ServletContext;<br />import javax.servlet.ServletException;<br />import javax.servlet.ServletOutputStream;<br />import javax.servlet.http.HttpServlet;<br />import javax.servlet.http.HttpServletRequest;<br />import javax.servlet.http.HttpServletResponse;<br />import javax.servlet.http.HttpServletResponseWrapper;<br />public class toHtml extends HttpServlet<br />{<br />public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException<br />{<br />String url="";<br />String name="";<br />ServletContext sc = getServletContext();<br />String file_name=request.getParameter("file_name");//你要訪問的jsp檔案,如index.jsp<br />//則你訪問這個servlet時加參數.如http://localhost/toHtml?file_name=index<br />url = "/"+file_name+".jsp";//這是你要產生HTML的jsp檔案,如<br />//http://localhost/index.jsp的執行結果.<br />name="/home/resin/resin-2.1.6/doc/"+file_name+".htm";//這是產生的html檔案名稱,如index.htm.<br />RequestDispatcher rd = sc.getRequestDispatcher(url);<br />final ByteArrayOutputStream os = new ByteArrayOutputStream();<br />final ServletOutputStream stream = new ServletOutputStream()<br />{<br />public void write(byte[] data, int offset, int length)<br />{<br />os.write(data, offset, length);<br />}<br />public void write(int b) throws IOException<br />{<br />os.write(b);<br />}<br />};<br />final PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));<br />HttpServletResponse rep = new HttpServletResponseWrapper(response)<br />{<br />public ServletOutputStream getOutputStream()<br />{<br />return stream;<br />}<br />public PrintWriter getWriter()<br />{<br />return pw;<br />}<br />};<br />rd.include(request, rep);<br />pw.flush();<br />FileOutputStream fos = new FileOutputStream(name); //把jsp輸出的內容寫到xxx.htm<br />os.writeTo(fos);<br />fos.close();<br />PrintWriter out=response.getWriter();<br />out.print("<p align=center><font size=3 color=red>首頁已經成功產生!Andrew</font></p>");<br />}<br />}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.