Convert jsp file content to html using servlet

Source: Internet
Author: User

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 "%>

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.