It is a good idea to automatically generate static pages, intercept jsp requests with filters, pre-process them first, and generate Html automatically. One easy way to think of is to direct the Request to a Servlet after the Filter intercepts the Request, send an http Request to the local machine in this Servlet, and then write the response to a file:
URLConnection urlConn = URLConnection. open (http: // localhost/req );
Avoid recursion.
Another method is to customize the Response instead of simulating http, and output the JSP Response returned by the server to our own Response. Then, we can quickly write the Response to the Html file and send it to the customer. In addition, because there is no http simulated request, the speed of Directly Reading the server's response is very fast.
The key to intercepting Response is to implement a WrappedResponse so that the server writes the Response to our WrappedResponse. This is similar to a proxy mode. Servlet 2.x already provides a WrappedResponse class. We only need to rewrite some of the key methods.
WrappedResponse implements the Response interface, which requires a Response as the constructor parameter. In fact, this is the application of the proxy mode: WrappedResponse acts as the proxy, it calls some methods of JSP/Servlet containers for preprocessing. We need to implement our own methods.
To sum up, the steps for using Filter to implement HTML buffering are as follows:
1. Use Filter to intercept requests, such as/a. jsp? Id = 123, ing to the corresponding html file name:/html/a.jspid1_123.htm.
2. Check whether/html/a.jspid1_123.htm exists. If yes, directly forward to this html and 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. When the next user sends the same request, step 3 is complete.
The advantage of using this method is that you do not need to change the existing Servlet or JSP page. The restriction is that the JSP page results cannot be related to the Session. You cannot use this method to buffer pages that need to be logged in or customized.