jsp生成靜態(tài)頁面的方法
更新時間:2007年02月07日 00:00:00 作者:
復制代碼 代碼如下:
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
public class toHtml extends HttpServlet
{
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String url="";
String name="";
ServletContext sc = getServletContext();
String file_name=request.getParameter("file_name");//你要訪問的jsp文件,如index.jsp
//則你訪問這個servlet時加參數(shù).如http://localhost/toHtml?file_name=index
url = "/"+file_name+".jsp";//這是你要生成HTML的jsp文件,如
//http://localhost/index.jsp的執(zhí)行結(jié)果.
name="/home/resin/resin-2.1.6/doc/"+file_name+".htm";//這是生成的html文件名,如index.htm.
RequestDispatcher rd = sc.getRequestDispatcher(url);
final ByteArrayOutputStream os = new ByteArrayOutputStream();
final ServletOutputStream stream = new ServletOutputStream()
{
public void write(byte[] data, int offset, int length)
{
os.write(data, offset, length);
}
public void write(int b) throws IOException
{
os.write(b);
}
};
final PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));
HttpServletResponse rep = new HttpServletResponseWrapper(response)
{
public ServletOutputStream getOutputStream()
{
return stream;
}
public PrintWriter getWriter()
{
return pw;
}
};
rd.include(request, rep);
pw.flush();
FileOutputStream fos = new FileOutputStream(name); //把jsp輸出的內(nèi)容寫到xxx.htm
os.writeTo(fos);
fos.close();
PrintWriter out=response.getWriter();
out.print("<p align=center><font size=3 color=red>首頁已經(jīng)成功生成!Andrew</font></p>");
}
}
相關(guān)文章
JSP/JAVABEAN+TOMCAT4.0.5+MYSQL組合建站總結(jié)
JSP/JAVABEAN+TOMCAT4.0.5+MYSQL組合建站總結(jié)...2006-10-10JavaWeb Servlet中url-pattern的使用
這篇文章主要介紹了JavaWeb Servlet中url-pattern的使用的相關(guān)資料,希望通過本文能幫助到大家,讓大家學習理解這部分內(nèi)容,需要的朋友可以參考下2017-10-10JSP上傳excel及excel插入至數(shù)據(jù)庫的方法
這篇文章主要介紹了JSP上傳excel及excel插入至數(shù)據(jù)庫的方法,涉及JSP文件上傳及針對excel的讀取、寫入數(shù)據(jù)庫等操作技巧,需要的朋友可以參考下2015-10-10淺談SpringMVC jsp前臺獲取參數(shù)的方式 EL表達式
下面小編就為大家分享一篇淺談SpringMVC jsp前臺獲取參數(shù)的方式 EL表達式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03jsp頁面顯示數(shù)據(jù)庫的數(shù)據(jù)信息表
本文主要介紹了jsp頁面顯示數(shù)據(jù)庫的數(shù)據(jù)信息表的實現(xiàn)方法。具有一定的參考價值,下面跟著小編一起來看下吧2017-01-01