Java design pattern-responsibility chain

Source: Internet
Author: User

Java design pattern-responsibility chain

Analyze the filters and interceptors in Java web

Filter: when a user requests a server, multiple filters can be added to filter requests,

Each filter has a different filtering process for the request.

Multiple filters form a filter chain.

After the filtering chain is complete, the server starts to process the request

Return result response after processing

Response will also be processed by the filter chain in reverse order

After completion, the returned user is shown as follows:


Code Section:

<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHByZSBjbGFzcz0 = "brush: java;"> Request and return object public class Request {public String RequestString;} public class Response {public String ResponseString ;} // Filter interface public interface Filter {public void doFilter (Request request, Response response, FilterChain chain);} Filter Implementation class public class HtmlFilter implements Filter {@ Overridepublic void doFilter (Request request Request, response response, FilterChain chain) {request. requestString = request. requestString. replace ("<","["). replace (">", "]"); System. out. println ("HtmlFilter request processing completed request. requestString = "+ request. requestString); chain. doFilter (request, response, chain); response. responseString = response. responseString + "| HtmlFilter response Processing"; System. out. println ("HtmlFilter response Return processing completed response. responseString = "+ response. responseString) ;}} public class SensitiveFilter implements Filter {@ Overridepublic void doFilter (Request request, Response response, FilterChain chain) {request. requestString = request. requestString. replace ("sensitive", "**"); System. out. println ("SensitiveFilter request processing completed request. requestString = "+ request. requestString); chain. doFilter (request, response, chain); response. responseString = response. responseString + "| SensitiveFilter response Processing"; System. out. println ("SensitiveFilter response Return processing completed response. responseString = "+ response. responseString) ;}} filter chain public class FilterChain {private List Filters = new ArrayList (); Public FilterChain addFilter (Filter filter) {filters. add (filter); return this;} int index =-1; public void doFilter (Request request, Response response, FilterChain chain) {if (filters. size ()> ++ index) {filters. get (index ). doFilter (request, response, chain);} else if (filters. size () = index) {response. responseString = "[server processing code]"; System. out. println ("returned after business logic processing:" + response. responseString) ;}} test class pu Blic class MainTest {public static void main (String [] args) {Request req = new Request (); req. RequestString = "O (empty _ blank) O Haha ~ <Script>

Related Article

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.