Configuring Filter and Dispatcher Mapping in Servlets

Introduction

Every web component, which is a part of a web application, needs to be configured in a common location or file under the payment directory of the project. By declaring all the components at a common location, the control flow among them is established so that all web components act in sync with the part of the Web application. This file is called the deployment descriptor of the application. So, as a Web component, the filter also needs to be declared inside the web.xml file. This is done by describing the attributes of a filter or filter chain within the XML elements inside the web.xml file. This process is called configuring filter.

The Filter Element

All the filters, as a part of a web application are configured in the web.xml file using the <filter> element and its sub-elements. These elements are.

  • <icon>: This specifies the locator of a small or a large image used to represent the filter in a GUI tool within a web application.
  • <filter-name>: This defines the name of the filter. This is used to refer to the filter definition somewhere in the deployment descriptor.
  • <display-name>: This element accepts the short name of the filter, which is intended to be displayed by GUI tools.
  • <description>: This provides a short description of the filter.
  • <filter-class>: This specifies the class name of the filter.
  • <init-param>: This holds the name and value pair of an initialization parameter.

Source code

<filter>
<icon>MysmallIcon.gif</icon>
<filter-name>LoggerFilter</filter-name>
<display-name>Filter</display-name>
<descritpion>This is my first filter</description>
<filter-class>myfilter.LoggerFilter</filter-class>
<init-param>
	<param-name>name</param-name>
	<param-value>ASHISH</param-value>
</init-param>
</filter>

This code shows a part of a typical web.xml file that has defined a filter. LoggerFilter inside it. The name of the filter class LoggerFilter is specified inside the <filter-name> element. The name of the filter to be displayed is written within the <display-name> element. The <description> element briefly describes the filter. Next, the <filter-class> element specifies the class name of the filter with its package name. Finally, the <init-param> element defines the name of the parameter to be passed and its value within <param-name> and <param-value> sub-elements respectively.

Filter Mapping

Filter mapping is a process of describing the location of a filter relative to other web components of a web application. It establishes a logical address for the filter with its path specified inside the web.xml file. For the purpose of filter mapping the web.xml has the following elements.

  • <filter-name>: The element inside the deployment descriptor holds the name of the filter to which a URL pattern or a servlet is mapped.
  • <url-pattern>: The element describes a pattern used to resolve URLs to which the filter applies.
  • <servlet-name>: The element specifies the name of a servlet whose request and response will be serviced by the filter.

Configuring Filter Chain

For a Web application with a filter chain, all the filters in the chain need to be configured in the web.xml file. The filter definition of the filters is given according to the control flow of requests and responses through them.

Source code

<filter>
<filter-name>SimpleFilter</filter-name>
<filter-class>SimpleFilter</filter-class>
</filter>
<filter>
<filter-name>ChainedFilter</filter-name>
<filter-class>ChainedFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ChainedFilter</filter-name>
<url-pattern>/index.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>ChainedFilter</filter-name>
<url-pattern>/Servlet</url-pattern>
</filter-mapping>

The source code illustrates the configuration of SimpleFilter and ChainFilter in a chain inside the web.xml file. In this case, the request is passed through the SimpleFilter first and then through the ChainFilter.

Dispatcher Mapping

In a web application, several servlet or JSP files work in sync with each other through the forward or include call. The request from the client needs to be forwarded to the next servlet from the first one. Any filter between a client and a servlet is invoked by the request call. Along with the request the forward and include can also invoke the filter.

In servlet specification 2.4, the filter is also configured for request dispatcher forward and include calls. This requires an additional <dispatcher>element within the <filter-mapping>element. This allows the filter to be invoked for a forward, include, or error call.

Source code

<filter-mapping>
<filter-name>LoggerFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>

The source code illustrates the use of the <dispatcher> element inside the <filter-mapping>element. The filter LoggerFilter is now invoked by the request for forward or include or error.

Summary

In web applications, all components are configured in a common file called the deployment descriptor (web.xml), ensuring synchronized control flow. Filters, like other web components, must be declared in this file by detailing their attributes within specific XML elements. Filters intercept requests between clients and servlets, and they can also be invoked during forward or include calls between servlets and JSP files. Servlet specification 2.4 introduced the <dispatcher> element within <filter-mapping> to enable filter invocation for forward, include, or error calls, enhancing the control over request handling.


Similar Articles