Use servlet to convert jsp file content into 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 called to process user requests only after successful initialization. The previous parameter provides methods and fields for accessing the initial request data,
* The next method provides servlet response construction.
*/
}
// 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 file name.
String pName = "e: \ Tomcat 5.5 \ webapps \ jspTohtml \ index.htm"; // generate the complete html path
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); // write the jsp output content to the htm file in the specified path
OS. writeTo (fos );
Fos. close ();
Response. sendRedirect (name); // go to the htm page after writing
}
}
Configure in the 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 index. jsp used for testing is as follows:
<% @ Page contentType = "text/html; charset = gb2312 "%>