Filter in Servlet

As of now, we have discussed Servlet Form data like getting and POST method. In this article we will discuss servlet Filters.what are filters, usage, advantages, etc.

What is Filter in Servlet?

Filter are classes in servlet. Before the client request is processed and after its processing object of Filter is invoked. Filter as the name suggests it is used to filter tasks such as conversion, logging, validation in servlet. The Entry of the filter is defined in the web.xml file. If any changes are applied we can change in web.xml file we don’t need to change the servlet.

Why we need Filter?

In the certain condition when we need to manage sessions in our application and to avoid redundant code in the application. To avoid this, we used Filter in Servlet. So, the filter is used in Server Side authentication and logging, etc.

Uses of Filter

  • A filter is used for validation.
  • A filter is used for encryption and decryption.
  • A filter is used for the recording of the incoming requests.
  • A filter class can be used to count the number of visitors for a site.

Filter API

The javax.servlet package contains three interfaces of Filter API.

  • Filter
  • FilterChain
  • FilterConfig

Filter Interface

Filter Interface is used to create a filter. It has all the life cycle methods for the filter. The following are the methods in a filter.

  • public void init(): This method is used to initialize the filter. it is invoked only once.
  • public void doFilter(HttpServletRequest request,HttpServletResponse response):This method is used to perform filtering task.It is invoked for every request.
  • public void destroys(): This method is invoked when the filter is destroyed. It is also invoked only once.

FilterChain Interface

The next filter or any resource is invoked by the FilterChain object. It contains only one method.

  • public void doFilter(HttpServletRequest request,HttpServletResponse response):This method passes the control to next filter or any resource such as servlet,etc.

FilterConfig Interface

The web container creates the object of FilterConfig. It is used to get the configuration information from the web.xml file. The following are the methods of FilterConfig Interface.

  • public void init(FilterConfig config): This method is used to initialize the filter. It is invoked only once.
  • public String getInitParameter(String parameterName): This method returns the parameter value for the specified parameter name.
  • public Enumeration getInitParameterNames():This method returns all the parameter names.
  • public ServletContext getServletContext():This method returns the ServletContext object.

Let’s see the Example of Filter in Servlet

In this example, we will see the validation of the login page in the Filter class.

index.html

FirstFilter.java

FirstServlet.java

web.xml

Output

Let’s see one more example of the filter where we will use FilterConfig Interface

web.xml

Example.java

Output

Advantages of Filter in Servlet

  • The filter analyse the user request before the servlet is called.
  • The filer can give an altered response.
  • The filter is pluggable.
  • The filter can help in interacting with external resources.