Obtain the html page file generated by jsp

Source: Internet
Author: User
Import java. io. ByteArrayOutputStream;
Import java. io. File;
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 ");
Url = "/JSP/wzgmanager/manageruser. jsp ";

// This is the generated HTML file name, such as index.htm.
Name = "/jsp/wzgManager/managerUser.doc ";
Name = getServletContext (). getRealPath (name );

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 ();

// Write the content of the jspoutput to xxx.htm.
File file = new file (name );
If (! File. exists ()){
File. createnewfile ();

}
System. Out. println (name );
Fileoutputstream Fos = new fileoutputstream (File );

OS. writeto (FOS );
FOS. Close ();

Response. setcontenttype ("text/html; charset = GBK ");
Printwriter out = response. getwriter ();
Out. Print ("<p align = center> <font size = 3 color = Red> the homepage is successfully generated! Andrew </font> </P> ");
}
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.