Copy codeThe Code is 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
// Add parameters when you access this servlet, such as http: // localhost/toHtml? File_name = index
Url = "/" + file_name + ". jsp"; // This is the jsp file you want to generate HTML, as shown in
// Http: // localhost/index. jsp execution result.
Name = "/home/resin-2.1.6/doc/" + file_name + ". htm"; // The generated HTML file name, 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 jspoutput content to xxx.htm
OS. writeTo (fos );
Fos. close ();
PrintWriter out = response. getWriter ();
Out. print ("<p align = center> <font size = 3 color = red> the homepage is successfully generated! Andrew </font> </p> ");
}
}