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 of this is to redirect a servlet after the filter has been intercepted, send an HTTP request to the native in this servlet, and then write the response to a file:
URLConnection urlconn = Urlconnection.open (http://localhost/req);
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.