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