Request and response objects representing user requests and responses can be obtained in the filter, so the Decorator pattern can be used in programming to wrap the request and response objects, and then pass the wrapped objects to the catalog.Target resources to achieve some special needs.
- I. Decorator design pattern
- Two, use Decorator design pattern to enhance request objects.
- Three, use Decorator design pattern to enhance response objects.
I. Decorator design pattern
1.1、DecoratorIntroduction of design patterns
When an object’s method does not meet the needs of business, there are usually 2 ways to enhance the method.
- Writing subclasses to cover the need to enhance.
- Use Decorator design pattern to enhance the method.
In Dr. Yan Hong’s book “JAVA and model”, the first thing to describe is the Decorator mode:The decoration mode is also called the Wrapper mode. Decorative patterns, which extend the functionality of objects in a transparent manner to the client, are an alternative to inheritance relationships.Decorative pattern is to dynamically extend the function of an object without changing the original class file and using inheritance. It is through the creation of a packaging object, that is, decoration to wrap the real object.
So when we need to enhance objects in practical applications, which method is better? There is no specific formula, it can only be used in specific ways according to specific needs.In one case, you have to use the Decorator design pattern: Enhanced objects that developers can only get their objects, not their class files。For example, request, response objects, which developers can manipulate through Sun’s HttpServletRequest response interface in servlets, are compiled by Tomcat server vendorsThe implementation class of request and response interface is written. When a web server calls a servlet, it creates objects with the implementation classes of these interfaces and passes them to the servlet program. In this case, developers do not know the R written by the server manufacturer at all.What is the implementation class of equest and response interface? You can only get the objects provided by the server vendor in the program, so you can only use the Decorator design pattern to enhance these objects.
1.2、DecoratorImplementation of design patterns
1.First look at what interfaces or parent classes the enhanced object inherits, and write a class to inherit those interfaces or parent classes as well.
2.A variable is defined in the class, and the type of variable is to enhance the type of the object.
3.Define a constructor in the class to receive objects that need to be enhanced.
4.Coverage needs to be enhanced, and enhanced code is written.
Two, use Decorator design pattern to enhance request objects.
Servlet API The default implementation class HttpServletRequestWrapper for the Decorator design pattern of a request object is provided, and the HttpServletRequestWrapper class implements the request connectionAll the methods in the port, but the internal implementation of these methods only calls the corresponding method of the wrapped request object to avoid the need for users to implement all the methods in the request interface when enhancing the request object.
2.1、Using Decorator mode to package request objects to solve Chinese garbled problem under get and post request mode
Write a filter CharacterEncodingFilter for handling Chinese garbled code. The code is as follows:
From the running results, we can see that the Chinese scrambling problem can be solved perfectly, whether it is get request or post request.
2.2、Using Decorator mode to wrap request objects to implement HTML tag escape function
Write a HTML escape filter. The code is as follows:
As you can see from the running results, all the HTML tags are exported.
2.3、Wrapping request objects in Decorator mode to implement sensitive character filtering
Write a sensitive character filter. The code is as follows:
Servlet API The default implementation class HttpServletResponseWrapper, which provides the Decorator design pattern for the response object, and the HttpServletResponseWrapper class, which implements the responseAll the methods in the interface, but the internal implementation of these methods only calls the corresponding method of the wrapped response object to avoid the need for users to implement all the methods in the response interface when they enhance the response object.
3.1、responseEnhance case — compress response text content
The HttpServletResponseWrapper object is applied to compress the response text content.
Specific idea: pass filter to the target page to pass a custom response object. In the custom response object, override the getOutputStream method and getWriter method so that the target resource calls this method to output the pageContent is obtained by our custom ServletOutputStream object. In our custom ServletOuputStream object, we override the write method to write the data out into a buffer. When page completion is lostAfter that, we can get the data written by the page in the filter, so we can call GzipOuputStream to compress the data and then write it to the browser to complete the compression function of the response positive file.
Compiling the compression filter is as follows:
For the data rarely updated in the page, such as commodity classification, in order to avoid querying the classification data from the database every time, the classification data can be cached in memory or files to reduce the database pressure and improve the system response speed.
Write a filter for cached data. The code is as follows: