Skip to content

Configure Filters

Configure Filters

The web.xml file is used to configure filters to resources, such as a servlet, JSP page, or Web application. This configuration helps to process the request and response objects. With the introduction of annotations in Servlet 3.0, filters can also be configured using annotations. Prior to the use of annotations, Deployment Descriptor was used for defining configuration and mapping for filters and servlets. The use of annotations eliminates the need of Deployment Descriptor. The mapping and configuration logic can be directly included within the filter class. Here we will discuss about:

  • Configure a filter using Deployment Descriptor (web.xml)
  • Configure a filter using Annotations

Configuring Filters using Deployment Descriptor (web.xml)

You can configure filters as part of a Web application by using the application’s web.xml Deployment Descriptor, which is located in the WEB-INF directory of the Web application. In Deployment Descriptor, you can declare and map the filter either to specific or multiple URL patterns or to servlets in the Web application. You can declare any number of filters and bind them with a specific pattern to any number of servlets or URL patterns. The <filter> and <filter-mapping> elements are used for configuring filters. The <filter> element must immediately succeed the <context-param> element and precede the <listener> and <servlet> elements in the web.xml file. The <filter> element declares a filter, defines a name for the filter, and specifies the Java class that executes the filter. One or more initialization parameters can also be specified inside the <filter> element by using the <init-param> element.

The following code snippet shows the code for adding a filter declaration in the web.xml file:

<filter>
     <filter-name>DemoFilter</filter-name>
     <display-name>demoFilter</display-name>
     <description>This is my demo filter</description>
     <filter-class>com.erainnovator.DemoFilter</filter-class>
     <init-param>
          <param-name>InitParamName</param-name>
          <param-value>InitParamValue</param-value>
     </init-param>
</filter>

In the preceding code snippet, the <filter-name> element specifies the name of the filter and the <filter-class> element specifies the Java class that executes the filter. The description and display name parameters are optional. The <init-param> element specifies the initialization parameters.

InitParamName is the name of the initialization parameter specified by the <param-name> element and InitParamValue is the value of the initialization parameter specified by the <param-value> element. The filter class in a Web application can read the initialization parameters by using the FilterConfig.getinitParameter() or FilterConfig.getinitParameterNames() method.

The <filter-mapping> element defines the filter that is to be executed on the basis of the URL pattern or filter-name specified within the <filter-mapping> element. You need to specify the name of the filter and the URL pattern for creating a filter mapping using a URL pattern. The <filter-mapping> element must immediately succeeded the <filter> element(s).
The following code snippet shows how to map a filter to a particular URL pattern:

<filter-mapping>
     <filter-name>DemoFilter</filter-name>
     <url-pattern>/myPattern/*</url-pattern>
<filter-mapping>

In the preceding code snippet, the DemoFilter filter is mapped to the requests containing the /myPattern/ URL pattern. According to Servlet 3.0 specification, the <filter-mapping> element supports multiple url patterns for a filter. The following code snippet shows mapping of a filter to multiple URL patterns:

<filter-mapping>
     <filter-name>DemoFilter</filter-name>
     <url-pattern>/myPattern/*</url-pattern>
     <url-pattern>/myDemoPatterns/*</url-pattern>
<filter-mapping>

In the preceding code snippet, the DemoFilter filter is mapped to the request containing /myPattern/ and /myDemoPattern/ URL patterns. Therefore, multiple URL patterns can be mapped to a single filter using the <url-pattern> element. Similarly, multiple filters can be mapped to a specific URL pattern. The filters can also be mapped to a specific servlet by mapping the filter to the name of the servlet that is registered in the Web application. The following code snippet shows the code for mapping a filter to the name of a servlet:

<filter-mapping>
     <filter-name>DemoFilter</filter-name>
     <servlet-name>DemoServlet</servlet-name>
<filter-mapping>

In the preceding code snippet, the DemoFilter filter is mapped to DemoServlet servlet. You can also map the filter to multiple servlets registered in a Web application, as shown in the following code snippet:

<filter-mapping>
     <filter-name>DemoFilter</filter-name>
     <servlet-name>DemoServlet</servlet-name>
     <servlet-name>MyServlet</servlet-name>
<filter-mapping>

In the preceding code snippet, the DemoFilter filter is mapped to the DemoServlet servlet and the MyServlet servlet. Apart from using Deployment Descriptor, filters can also be configured by using annotations.

Configuring Filters Using Annotations

With the introduction of annotations, the configuration code can be directly included in the code of the filter class. The @WebFilter annotation is used to mark a filter.

The following code snippet shows how a filter can be configured using annotations:

@webFilter(filterName= “DemoFilter”, urlpatterns={ “/myPattern/*”})

In the preceding code snippet, the filterName attribute defines the name of the filter and urlPatterns attribute defines the URL pattern which causes the invocation of the filter.

nv-author-image

Era Innovator

Era Innovator is a growing Technical Information Provider and a Web and App development company in India that offers clients ceaseless experience. Here you can find all the latest Tech related content which will help you in your daily needs.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.