Filter filters are mainly used to filter data transmitted to the background at the front end. For example, JSP, Servlet, static images or HTML are intercepted to control access permissions, filter sensitive words, and compress corresponding information.
The following describes how to set Request Encoding in batches (Other Work codes are similar and will not be listed one by one)
Java
1. public class encodingfilter implements filter {2. 3. private string encoding = NULL; 4. 5. public void destroy () {6. encoding = NULL; 7 .} 8. 9. public void dofilter (servletrequest request, servletresponse response, 10. filterchain chain) throws ioexception, servletexception {11. string encoding = getencoding (); 12. if (encoding = NULL) {13. encoding = "gb2312"; 14 .} 15. request. setcharacterencoding (encoding); // specify the encoding 16 in the request. chain. dofilter (request, response); 17 .} 18. 19. public void Init (filterconfig) throws servletexception {20. this. encoding = filterconfig. getinitparameter ("encoding"); 21 .} 22. 23. private string getencoding () {24. return this. encoding; 25 .} 26.
Configure XML
1.<filter> 2. <filter-name>EncodingFilter</filter-name> 3. <filter-class>com.logcd.filter.EncodingFilter</filter-class> 4. <init-param> 5. <param-name>encoding</param-name> 6. <param-value>gb2312</param-value> 7. </init-param> 8.</filter> 9. 10.<filter-mapping> 11. <filter-name>EncodingFilter</filter-name> 12. <url-pattern>/*</url-pattern> 13.</filter-mapping>
Responsibility Chain Model
The responsibility chain mode is an object behavior mode. In the responsibility chain mode, many objects are connected by each object's reference to its next home to form a chain. Requests are transmitted on this chain until an object on the chain decides to process this request. The client that sends this request does not know which object on the chain will eventually process this request, which allows the system to dynamically reorganize and assign responsibilities without affecting the client.
The classic application of the responsibility chain model is the filter technology in servlet. Chain the sender data.