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 />}