Hello! I am a beginner in the use of web technologies in Java .. I came across this code on the Internet .. I can not understand what is the point in creating filters, for example, like this one.

The doFilter() method of the Filter Chain class is not clear. The documentation says: What is this method:

In the chain of water, it can be invoked

Perhaps the question is stupid, but because of the lack of understanding of absolutely this topic I wanted to ask: What is the next filter in turn? (What does it mean to wake up as I understand it when translating into Russian) "Next filter"? Any examples will help to understand the very idea of ​​this functionality .. References on the topic if anyone can advise, too, will help!

 public class AuthFilter implements Filter { private FilterConfig filterConfig; @Override public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("doFilter is execute"); HttpServletResponse httpResponse = (HttpServletResponse) response; httpResponse.sendRedirect("login.jsp"); //chain.doFilter(request, response); } @Override public void destroy() { this.filterConfig = null; } } 
  • In general, if you want to make a web in Java, sit down and thoughtfully, slowly read the spec . There everything is disassembled in detail what and for what. - Nofate
  • I will read thanks. - Maks.Burkov

2 answers 2

Filters in the Servlet API are a mechanism that allows you to:

  • pre-process a request for certain URLs before it is passed to the servlet;
  • and / or process the response before returning to the client;
  • or return some answer forcibly.

Filters, like servlets, are assigned a mask of URLs for which the filter will be applied. For example, /* or /admin/* . In addition, filters are declared in a specific order.

The actual processing takes place in the doFilter() method, which you must implement. Because There may be several filters matching the URL of the incoming request, they are arranged in a chain (filter chain), according to the specified order. Therefore, after completing your task, the filter must call the chain.doFilter(request, response) method to push the request to the next receiver (filter or end servlet).


Typical examples of the use of filters:

  • authentication and authorization;
  • enrichment of the request or response with additional headers;
  • encryption and compression;
  • data format conversion;
  • caching;
  • modification of XML streams.

For example, in a web application, all requests can be encoded with one filter, and the other will check access to the protected part (for example, to the admin panel). So part of the requests will be passed through two filters, and the rest - only through one.


In your example, all requests that go through this filter will be unconditionally redirected to the login.jsp page. If at the end of the doFilter() method there was something like this:

 if (userAuthorized()) { chain.doFilter(request, response); } else { httpResponse.sendRedirect("login.jsp"); } 

it would have turned out to be a simple authorization filter, which sent requests from unregistered users to login.jsp , and skipped the rest.

  • Nofate Thank you very much for your answers .. \ - Maks.Burkov

Filters are reusable Java code that allows you to convert the content of HTTP requests, HTTP responses, and information contained in HTML headers. The filter is engaged in preprocessing the request before it enters the servlet, and / or the subsequent processing of the response coming from the servlet. For example, often filters are used to establish the encoding in the request and response parameters. Filters are described in web.xml and are linked to URL requests. If several urls match several filters, they will be applied in the order listed in the web.xml. The filter contains the init and destroy methods to initialize / delete the filter and its parameters (if necessary) and the main doFilter method. If the filter is applied to a certain URL that corresponds to a certain servlet, then the request goes to the doFilter filter method, they do something with it, then they transfer the request further along the chain (doFilter method) to the next filter (if any) or to the servlet . After the servlet returns a response, the last in the filter chain in the opposite direction leaves the doFilter methods and at the end is sent to the client.