Servlet JSP Two-weight cultivation: Filter Filters

Source: Internet
Author: User

Original website:

Abstract: Original creation Place: brick slurry Carpenter hope reprint, keep abstract, thank you!

A true friend is when you blind everyone's eyes, you can see through your true appearance and heart pain. Objective

I haven't written a blog for a long time. haha ~ blades is not old Ah, the pen to prepare 11.11 suit to an article. All say restudying, this sentence is absolutely correct. According to the human memory curve, temperature is a good way to consolidate memory. It's like I'm always flipping through my blog. Look back and see what's new. That would be enough.

from right to left: the realm of Life also

Filter filters

In fact, the interceptor, I wrote an article in posted @ 2014-03-20 12:45: "⑧javaweb in the example of learning (filter filters)." Look over the head and find that it's not too fine-grained. But there are two small examples of applications that are pretty good. If you like to see examples, you can go and see.

What is a filter?

In fact, the filter and the servlet are very similar, are Java components. That is, you can use it to intercept and process requests before sending the servlet, or you can handle the response before the response is sent back, after the Serlvet has finished working. Thus, as in the previous article, the filter filter is an object that intercepts the request and processes the ServletRequest or servletresponse to the requested resource. Simple description of how it works

What the filter does

This is what we would most like to see, in fact, filter can be used as a plug-in component. It works between the request and the response.

the role of the request filter:

    • Complete Security Checks
    • Reformat request header or body
    • Create request audits and logs

the role of the response filter:

    • Compress the corresponding stream
    • Append or modify a stream
    • Create a completely different response

Case Code Explanation

A filter interface is provided in the Servlet API, and if the Java class is written to implement this interface, it is called the filter filter. The implementation class of filter implements a Java EE filter, the filter API allows access to the ServletContext, and can also be linked to another filter.

Filter is similar to a servlet, and has a life cycle. There are: Init (), Destroy (), and a core method: DoFilter ().

    • Init (Filterconfig): This is the initialization method of filter, which is called when the servlet container creates a filter instance. In this method, the initialization parameters of the filter in the Web. xml file can be read
    • DoFilter (Servletrequest,servletresponse,filterchain): This method completes the actual filtering operation, when the client requests access to the URL associated with the filter, The servlet container will first call the filter's Dofilter method. The Filterchain parameter is used to access subsequent filters
    • Destroy (): The servlet container calls this method before destroying the filter instance, which frees the resource used by the filter

Filter execution mechanism diagram:

Here we implement this interface:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 Public class demofilterimplements filter {     @Override      public void init (filterconfig filterconfig) throws servletexception      {    }        @Override     public void  dofilter (ServletRequest request, servletresponse response,              filterchain chain) throws ioexception, Servletexception     {         chain.dofilter (request, response);    &NBSP,}        @Override     public void  Destroy ()     {        //complete cleanup work      }  }

DoFilter (ServletRequest request, servletresponse response, Filterchain chain)

We mainly look at this parameter: Servletrequest/servletresponse. This method does not take the HTTP request and the corresponding object to do parameters, but only the regular Servletrequest/servletresponse object. Here is the universal design, filter design to general, if the filter is used for a non-web application, so implement a non-HTTP interface. But don't worry, Servletresponse is HttpServletResponse's Super class.

Design of filter

From the filter execution mechanism diagram, we can realize that its design is much like a thing---"stack". A picture I can extract from my book: Filter3→filter7→serlveta→filter7→filter3


Reread the accumulation of filter.

if the above articles or links are helpful to you, don't forget to click the "Like a" button on the article button or in the bottom right corner of the page. You can also click on the right side of the page "share" hover button oh, let more people read this article

Here is the PDF, to be downloaded please open (

Servlet JSP Two-weight cultivation: Filter Filters

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: 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.