Copy Code code as follows:
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");/the JSP file you want to access, such as index.jsp
You add parameters to the servlet when you visit it. such as Http://localhost/toHtml?file_name=index
url = "/" +file_name+ ". jsp";//This is the JSP file you want to generate HTML for, such as
The results of http://localhost/index.jsp execution.
Name= "/home/resin/resin-2.1.6/doc/" +file_name+ ". htm";//This is the generated HTML filename, such as 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); Write the contents of JSP output to xxx.htm
Os.writeto (FOS);
Fos.close ();
PrintWriter Out=response.getwriter ();
Out.print ("<p align=center><font size=3 color=red> Home has been successfully generated!" Andrew</font></p> ");
}
}