Use servlet to convert JSP file contents to HTML

Source: Internet
Author: User
Tags final http post return string
Use servlet to convert JSP file contents to HTML.

Use servlet to convert JSP file contents to HTML. The code is as follows:

Package examples;
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 {
private static final String Content_Type = "text/html;" CHARSET=GBK ";
Initialize Global Variables
public void Init () throws Servletexception {
}

Process the HTTP GET request
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Response.setcontenttype (Content_Type);
Service (request, response);
/**
* This method can be invoked to process user requests only after successful initialization. The previous parameter provides methods and fields to access the initial request data.
* The latter provides a way for the servlet to construct the response.
*/
}
Process the HTTP Post request
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Doget (request, response);
}
public void Destroy () {
}
public void Service (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {

ServletContext sc = Getservletcontext ();
String url = "/index.jsp";

String name = "Index.htm"; This is the generated HTML filename

String pname = "E:\\tomcat 5.5\\webapps\\jsptohtml\\index.htm"; Full path to generate HTML
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 (pname); Writes the contents of the JSP output to the HTM file in the specified path
Os.writeto (FOS);
Fos.close ();
Response.sendredirect (name); Turn to HTM page when finished writing
}
}

Configure in the Web.xml file:

< servlet> < servlet-name>tohtml < Servlet-class>examples. tohtml < servlet-mapping> < servlet-name>tohtml < url-pattern>/tohtml

Here is the index.jsp for testing:

<%@ page contenttype= "text/html; charset=gb2312 "%>< html> < head> < Title>cache Filter test < meta http-equiv = "Content-type" content= "text/html"; charset=gb2312 "> < body> simple test:s=<% int s=0;   Mock time-consuming code for (int i=0;i< 1000;i++) {for (int j=0;j< 1000;j++) {s=s+j; }}out.print (s);%>



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.