Filter in servlets

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

Hello, till now we have learned about listeners in servlets. This article will help understand the filter in servlets.

What is the need?

  • We may need to log the requests coming for resource
  • Validate the data in request before sending the request to resource

How can it be achieved?

Servlet API provides the concept called filters which are able to intercept the requests before they can reached the requested resource. We need to create our own filter by implementing Filter interface.

The Filter Interface:

This is what javadoc says about Filter in servlets.

A filter is an object that performs filtering tasks on either the request to a resource (a servlet or static content), or on the response from a resource, or both.

There are three methods those need to be implemented in creating our own filter. According to the javadocs,

Init method.

Called by the web container to indicate to a filter that it is being placed into service.

We can write code in init method to get a reference to FilterConfig object which is like  ServletConfig  object and can read initialization parameters declared in web.xml.

doFilter method.

The doFilter method of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain. The FilterChain passed in to this method allows the Filter to pass on the request and response to the next entity in the chain.

All our business logic for what filter is created goes into this method. For example, you can place log statements so that we can know after the resource is served and again filter is called after serving the resource.

destroy method

Called by the web container to indicate to a filter that it is being taken out of service.

Step 1: Create a dynamic web project.

First Create a new dynamic web project.

Step 2: Create a HelloServlet.

Simply create a HelloServlet which simply prints a message.

 Step 3: Create logger filter.

Create a filter that we will use for logging as follows.

Step 4: Configure filters and servlets

Configure servlets and filters in web.xml as follows

Here notice that, the LoggerFilter is mapped to HelloServlet in filter-mapping tag. That means this filter is applied whenever a request for a servlet with name HelloServlet is made.

Step 5: Run the example.

Deploy your application under tomcat server and hit url http://localhost:8080/ServletsJSPExamples/HelloServlet . You will see the messages as follows.

 

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

4 thoughts on “Filter in servlets

  • Pingback:Filter order in servlets. | theJavaGeek

  • March 4, 2016 at 2:08 pm
    Permalink

    this program is running, but i don’t see the messages “Before visiting the servlet” and “after visiting the servlet”. where are they supposed to get printed?? in the console also i only see “We are in “HelloServlet”.

    Reply
    • March 4, 2016 at 2:21 pm
      Permalink

      Hi Juhi, thank you for your comment. What is the web.xml configuration you’ve done for filter?

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *