Convert dynamic page to static page in JSP

Source: Internet
Author: User
Original: http://www.gpxz.com/diannao/biancheng/261225.html

To automatically generate static pages, it is a good idea to intercept JSP requests with filter and preprocess them first to generate HTML automatically. An easy way to think about this is when filter intercepts request, directs a servlet, sends an HTTP request to the native in this servlet, and then writes the response to a file:

URLConnection urlconn = Urlconnection.open ();

Be careful to avoid recursion.

Another method is to not simulate HTTP, but to customize the response, the JSP response returned by the server to our own response, we can quickly write the response to the HTML file, and then sent to the customer. Also, the direct read server response is very fast because there is no HTTP impersonation request.

The key to intercepting response is to implement a wrappedresponse that will let the server write the response into our wrappedresponse. This is similar to a proxy mode, and the Servlet 2.x already provides a wrappedresponse class, and we just need to copy some of the key methods.

Wrappedresponse implements the response interface, which requires a response as a constructor parameter, which is actually the application of the proxy pattern: Wrappedresponse acts as the proxy role, which will jsp/ Some of the method calls of the servlet container are preprocessed, and we need to implement our own methods.

Comprehensive: The steps of using filter to implement HTML buffering are:

1. Use the filter interception request, such as/a.jsp?id=123, to map to the corresponding HTML file name/html/a.jspid=123.htm.

2. Find out whether there are/html/a.jspid=123.htm, if any, directly forward to this HTML, end.

3. If not, implement a wrappedresponse and then call Filterchain (Request, Wrappedresponse).

4. Write the returned wrappedresponse to the file/html/a.jspid=123.htm, and then return the response to the user.

5. The next time the user sends the same request, the 2nd step is over.

The advantage of using this method is that you do not need to change the existing servlet,jsp page, the limit is that the JSP page results can not be related to the session, the need to log on or user-customized pages can not be buffered in this way.

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.