Java Responsibility Mode simulation processing Reques Response

Source: Internet
Author: User

First, the goal

1. Use the filter simulation to process request, Response

2. Detailed tips:

(1) The Dofilter method of the filter is changed to Dofilter (Request,resopnse,filterchain) and has a filterchain reference to prepare to invoke the next filter with Filterchain

(2) Filterchain inherits the filter, so that Filterchain is both Filterchain and filter, then Filterchain can invoke the filter method Dofilter (Request,resopnse, Filterchain)

(3) In Filterchain's Dofilter (Request,resopnse,filterchain), there is an index tag that executes to the first filter, and when all the filter has been executed, the Request is processed and the return is Continue to perform response processing in reverse order

Second, the Code

1.filter.java

2.htmlfilter.java

3.sensitivefilter.java

4.filterchian.java

5.request.java

6.response.java

7.test.java

1.filter.java

1  Public Interface Filter {2      Public void DoFilter (Request request, Response Response, Filterchain filterchain); 3 }

2.htmlfilter.java

1  Public classHtmlfilterImplementsFilter {2 3 @Override4      Public voidDoFilter (Request request, Response Response,5 Filterchain Filterchain) {6Request.setrequeststr (Request.getrequeststr (). Replace (' < ', ' ['). Replace (">", "]") + "---htmlfilter ()");7 Filterchain.dofilter (Request, response, filterchain);8Response.setresponsestr (RESPONSE.GETRESPONSESTR () + "---htmlfilter ()");9     }Ten  One}

3.sensitivefilter.java

1  Public classSensitivefilterImplementsFilter {2 3 @Override4      Public voidDoFilter (Request request, Response Response,5 Filterchain Filterchain) {6Request.setrequeststr (Request.getrequeststr (). Replace ("sensitive", "happiness") + "---sensitivefilter ()");7 Filterchain.dofilter (Request, response, filterchain);8Response.setresponsestr (RESPONSE.GETRESPONSESTR () + "---sensitivefilter ()");9     }Ten  One}

4.filterchian.java

1  Public classFilterchainImplementsFilter {2 3     Privatelist<filter> filters =NewArraylist<filter>();4     intindex = 0;//tag execution to the first few filter5     6     //set the return value of the function to Filterchain, and return this to make it easy to write code in a chained style7      PublicFilterchain AddFilter (filter filter) {8 Filters.add (filter);9         return  This;Ten     } One  A      Public voidDoFilter (Request request, Response Response, Filterchain FC) { -         if(Index = = filters.size ())return ; -Filter f =Filters.get (index); theindex++; - F.dofilter (Request, response, FC); -     } -  +}

5.request.java

1  Public classRequest {2 3     PrivateString requeststr;4 5      PublicString getrequeststr () {6         returnRequeststr;7     }8 9      Public voidsetrequeststr (String requeststr) {Ten          This. Requeststr =Requeststr; One     } A      -}

6.response.java

1  Public classResponse {2 3     PrivateString responsestr;4 5      PublicString getresponsestr () {6         returnResponsestr;7     }8 9      Public voidsetresponsestr (String responsestr) {Ten          This. Responsestr =Responsestr; One     } A      -}

7.test.java

1  Public classTest {2 3 @org. Junit.test4      Public voidTestfilter () {5         6String msg = "; 7         8Request Request =NewRequest ();9 request.setrequeststr (msg);TenResponse Response =NewResponse (); OneRESPONSE.SETRESPONSESTR ("Response------------"); A          -Filterchain FC =NewFilterchain (); -Fc.addfilter (NewHtmlfilter ()). addfilter (NewSensitivefilter ()); the          - Fc.dofilter (Request, response, FC); - System.out.println (Request.getrequeststr ()); - System.out.println (Response.getresponsestr ()); +          -     } +  A}

III. Results of implementation

Java Responsibility Mode simulation processing Reques Response

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.