Responsibility chain or Filter mode-using the filter chain

Source: Internet
Author: User

//Filter Interface Public InterfaceFilter { Public voiddoFilter (Request req,response res,filterchain c); }
//client-delivered content to the server//request in the simulated Javaweb Public classRequest { PublicString Requeststr; }//Server feedback content to the client//simulating the response in Javaweb Public classResponse { PublicString Responsestr; }//Filters for HTML Public classHtmlfilter implements filter{@Override Public voidDoFilter (Request req, Response Res,filterchain FC) {Req.requeststr=req.requeststr.replace ("<","["); Req.requeststr=req.requeststr.replace (">","]")+"--htmlfilter"; Fc.dofilter (req, res, FC);
//here it invokes the next filter in the filtering chain//Fc.dofilter (req, res, FC) after the entire filter chain has been processed.//after the processing is done, the following method will be executed res.responsestr+= "--htmlfilter"; //then the filter in turn executes this line of code, and it turns out to be executed in reverse order .res.responsestr+="--htmlfilter"; } } //filters for sensitive words Public classSensitivefilter implements filter{@Override Public voidDoFilter (Request req, Response Res,filterchain FC) {Req.requeststr=req.requeststr.replace ("Sensitive","Mingan")+"--sensitivefilter"; Fc.dofilter (req, res, FC); Res.responsestr+="--sensitivefilter"; } } //Filter Chain Public classFilterchain implements filter{PrivateList<filter> list=NewArraylist<filter>(); PublicFilterchain AddFilter (filter filter) {List.add (filter); return This; } intindex=0; @Override Public voidDoFilter (Request req, Response Res,filterchain FC) {if(Index==list.size ())return; Filter F=list.Get(index); Index++; //execute the next filter in turn until the entire filter chain has finished executingF.dofilter (req, res, FC); } } Public classClient {/** Request: Information sent to the server by the client * Response: Server feedback to the client information * The function to be implemented is: the filter that the Request carries with the information that passes through is with the information that Response carries Over the filter opposite*/ Public Static voidMain (string[] args) {Request req=NewRequest (); Response Res=NewResponse (); Req.requeststr="This is one of my sensitive links <a> Youku </a>"; Res.responsestr="Response"; Filterchain FC=NewFilterchain (); Fc.addfilter (NewHtmlfilter ()); Filterchain FC1=NewFilterchain (); Fc1.addfilter (NewSensitivefilter ()); Fc.addfilter (FC1);//Add a filter chain over a filter chainFc.dofilter (req, RES,FC); System. out. println (REQ.REQUESTSTR); System. out. println (RES.RESPONSESTR); } /** * Execution Result: * This is one of my Mingan links [a] Youku [/a]--htmlfilter--sensitivefilter * response--sensitivefilter--htmlfilte R*/ }

Responsibility chain or Filter mode-using the filter chain

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.