Tomcat filter Simulation

Source: Internet
Author: User

Directly use the code filter. Java

Public interface filter {// filter public void dofilter (request, response, filterchain chain );}

Filterchain. Java

Public class filterchain {// store all the filters private list <filter> filters; // record the Current Filter index private int index; // The filter chain initializes public filterchain () {filters = new arraylist <filter> (10);} public void addfilter (filter) {filters. add (filter);}/*** iterative filtering */Public void dofilter (request, response) {If (index> = filters. size () return; filters. get (index ++ ). dofilter (request, response, this );}}

Request. Java

public class Request {        private String value;    public String getValue() {        return value;    }    public void setValue(String value) {        this.value = value;    }        @Override    public String toString() {        return this.value;    }}

Response. Java

public class Response {        private String value;    public String getValue() {        return value;    }    public void setValue(String value) {        this.value = value;    }        @Override    public String toString() {        return this.value;    }}

Filterone. Java

Public class filterone implements filter {/*** filter the fuck character */@ override public void dofilter (request, response, filterchain chain) {system. out. println ("go to the filter filterone -------------------------------------"); // execute the next filter chain. dofilter (request, response); system. out. println ("out Filter filterone ---------------------------------------");}}

Filtertwo. Java

Public class filtertwo implements filter {@ override public void dofilter (request, response, filterchain chain) {system. out. println ("go to the filter filtertwo -------------------------------------"); // execute the next filter chain. dofilter (request, response); system. out. println ("out Filter filtertwo ---------------------------------------");}}

Test. Java

public class Test {        public static void main(String[] args) {        FilterChain chain = new FilterChain();        chain.addFilter(new FilterOne());        chain.addFilter(new FilterTwo());        chain.doFilter(new Request(), new Response() );    }}

Running result

Go to the filter filterone plugin and enter the filter filtertwo plugin. Go out of the filter filtertwo ------------------------------- go out of the filter filterone ---------------------------------------

 

Tomcat filter Simulation

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.