Java filter and accountability chain Design Pattern

Source: Internet
Author: User

 

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.

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.