Simulate the Filter of JavaEE

Source: Internet
Author: User

Simulate the Filter of JavaEE
After several years of development in JavaEE, I only know the execution processes of filters and servlets, but I still don't know how to implement filters. FilterChain in Filter processing is a J2ee interface and does not provide implementation. The specific implementation is implemented by various Servlet containers. Today I found a time. I briefly checked the internal implementation of Tomcat's FilterChain and gave me a general understanding. I will simulate it below. Based on the simple implementation of filters in Tomcat, the following class diagrams are designed: Filter interface and FilterChain, which are completely designed according to the Filter interface and FilterChain in J2EE. FilterContext is the execution context of the Filter. DefaultFilterChain is used to provision the execution of each filter. Request is a simple encapsulated Request. FirstFilter and SecondFilter are used to simulate the custom Filter during development. Client is used for testing. The following code is attached: copy the code // J2EE Filter/*** Created with IntelliJ IDEA. * User: Administrator * Date: 14-7-28 * To change this template use File | Settings | File Templates. */public interface Filter {public void init (FilterConfig cfg); public void doFilter (Request request, Object response, FilterChain chain); public void destroy ();} // J2EE FilterChian public interface FilterChain {void doFilter (Request Request, Object response);} // Filter configuration information, used for the Filter initialization public class FilterConfig {private String name; public FilterConfig () {} public FilterConfig (String name) {this. name = name;} public String getName () {return name;} public void setName (String name) {this. name = name ;}// simulate the context of the Filter execution public class FilterContext {List <Filter> filters = new ArrayList <Filter> (); public FilterContext () {} pu Blic void addFilter (Filter filter, FilterConfig cfg) {this. filters. add (filter); filter. init (cfg);} public void remvoeFilter (Filter filter) {this. filters. remove (filter); filter. destroy ();} public Filter getFilter (int index) {return filters. get (index) ;}/// custom Request public class Request {private String url; private String param; public String getUrl () {return url ;} public void setUrl (String url) {This. url = url;} public String getParam () {return param;} public void setParam (String param) {this. param = param;} @ Override public String toString () {return this. url + "? "+ This. getParam () ;}}// simulate the internal implementation of the FilterChain public class DefaultFilterChain implements FilterChain {private FilterContext context; private int I = 0; public void doFilter (Request request Request, Object response) {if (I <context. filters. size () {context. getFilter (I ++ ). doFilter (request, response, this);} else {System. out. println ("the filter has been processed. Now the Servlet is used to process the request... ") ;}} Public void setContext (FilterContext context) {this. context = context ;}} copies several classes above the code to simulate J2EE's FIlter design. Next, customize two filters: copy the code public class FirstFilter implements Filter {private String name; public void init (FilterConfig cfg) {this. name = cfg. getName (); System. out. println ("initialization" + cfg. getName ();} public void doFilter (Request request, Object response, FilterChain chain) {System. out. println (this. name + ":" + request); if (request. getUrl ()! = Null) {chain. doFilter (request, response) ;}} public void destroy () {// To change body of implemented methods use File | Settings | File Templates .}} public class SecondFilter implements Filter {private String name; public void init (FilterConfig cfg) {this. name = cfg. getName (); System. out. println ("initialization" + cfg. getName ();} public void doFilter (Request request, Object response, FilterChain chain ){ System. out. println (this. name + ":" + request); if (request. getUrl ()! = Null) {chain. doFilter (request, response) ;}} public void destroy () {// To change body of implemented methods use File | Settings | File Templates .}} copy the code to simulate sending a request from the Client: copy the code public class Client {public static void main (String [] args) throws InterruptedException {final FilterContext context = init (); final int poolsize = 10; // simulate multiple request ExecutorService pool = Executors. newFixedThreadPool (poolsize); List <Calla Ble <Object> tasks = new ArrayList <Callable <Object> (poolsize); for (int I = 0; I <poolsize; I ++) {tasks. add (new Callable <Object> () {public Object call () throws Exception {// client Request request = new Request (); request. setUrl ("http: // localhost: 8080/filter/hello... "); request. setParam ("name = zhangsan & age = 23 & address = xxxxx"); Object response = new Object (); // The simulation filter filters DefaultFilterChain chain = new Defa requests. UltFilterChain (); chain. setContext (context); chain. doFilter (request, response); return response ;}});} pool. invokeAll (tasks);} // initialize FilterContext static FilterContext init () {// initialize FilterContext and add a custom Filter, this process simulates the startup of FilterContext context = new FilterContext (); Filter filter Filter = new FirstFilter (); context. addFilter (filter, new FilterConfig ("firstFilter"); filter = new SecondFilter (); contex T. addFilter (filter, new FilterConfig ("secondFilter"); return context ;}} copies the code to simulate the process of sending a request and filtering the request. No response information is processed. Execution result of the above simulation: Copy code initialization firstFilter initialization secondFilter firstFilter: http: // localhost: 8080/filter/hello ...? Name = zhangsan & age = 23 & address = xxxxx secondFilter: http: // localhost: 8080/filter/hello ...? Name = zhangsan & age = 23 & address = xxxxx the filter has been processed. Now the Servlet processes the request... FirstFilter: http: // localhost: 8080/filter/hello ...? Name = zhangsan & age = 23 & address = xxxxx secondFilter: http: // localhost: 8080/filter/hello ...? Name = zhangsan & age = 23 & address = xxxxx the filter has been processed. Now the Servlet processes the request... FirstFilter: http: // localhost: 8080/filter/hello ...? Name = zhangsan & age = 23 & address = xxxxx secondFilter: http: // localhost: 8080/filter/hello ...? Name = zhangsan & age = 23 & address = xxxxx the filter has been processed. Now the Servlet processes the request... FirstFilter: http: // localhost: 8080/filter/hello ...? Name = zhangsan & age = 23 & address = xxxxx firstFilter: http: // localhost: 8080/filter/hello ...? Name = zhangsan & age = 23 & address = xxxxx secondFilter: http: // localhost: 8080/filter/hello ...? Name = zhangsan & age = 23 & address = xxxxx secondFilter: http: // localhost: 8080/filter/hello ...? Name = zhangsan & age = 23 & address = xxxxx the filter has been processed. Now the Servlet processes the request... The filter has been processed. Now the Servlet is used to process the request... FirstFilter: http: // localhost: 8080/filter/hello ...? Name = zhangsan & age = 23 & address = xxxxx secondFilter: http: // localhost: 8080/filter/hello ...? Name = zhangsan & age = 23 & address = xxxxx the filter has been processed. Now the Servlet processes the request... FirstFilter: http: // localhost: 8080/filter/hello ...? Name = zhangsan & age = 23 & address = xxxxx secondFilter: http: // localhost: 8080/filter/hello ...? Name = zhangsan & age = 23 & address = xxxxx the filter has been processed. Now the Servlet processes the request... FirstFilter: http: // localhost: 8080/filter/hello ...? Name = zhangsan & age = 23 & address = xxxxx firstFilter: http: // localhost: 8080/filter/hello ...? Name = zhangsan & age = 23 & address = xxxxx firstFilter: http: // localhost: 8080/filter/hello ...? Name = zhangsan & age = 23 & address = xxxxx secondFilter: http: // localhost: 8080/filter/hello ...? Name = zhangsan & age = 23 & address = xxxxx secondFilter: http: // localhost: 8080/filter/hello ...? Name = zhangsan & age = 23 & address = xxxxx secondFilter: http: // localhost: 8080/filter/hello ...? Name = zhangsan & age = 23 & address = xxxxx the filter has been processed. Now the Servlet processes the request... The filter has been processed. Now the Servlet is used to process the request... The filter has been processed. Now the Servlet is used to process the request... Copy the code above is the simulation of the Filter, the specific process, you need to carefully study the source code to know. However, this simulation provides at least a simple understanding of Filter processing.

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.