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 V OID Doget (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException {respon
Se.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, and the latter provides a way for the servlet to construct the response. *//Process the HTTP Post request public void DoPost (HttpServletRequest request, httpservletresponse response) t
Hrows servletexception, IOException {doget (request, response); public void Destroy () {} public void service (HttpServletRequest request, httpservletresponse response) throws Ser
Vletexception, 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); After writing, turn to HTM page} and configure in Web.xml file: <servlet>
<servlet-name>Tohtml</servlet-name>
<servlet-class>examples. Tohtml</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Tohtml</servlet-name>
<url-pattern>/Tohtml</url-pattern>
</servlet-mapping>
The following is used to test the index.jsp: <%@ page contenttype= "text/html;" charset=gb2312 "%>
<title>cache Filter test</title>
<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);
%>
</body>
Effect Chart: