VOOZH about

URL: https://www.geeksforgeeks.org/java/servlet-filterconfig/

⇱ Servlet - FilterConfig - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Servlet - FilterConfig

Last Updated : 13 May, 2026

FilterConfig is an object created by the web container for each filter to access initialization parameters defined in the web.xml file. It helps separate configuration details from filter implementation, making web applications easier to manage and maintain.

  • Used to read filter initialization parameters from web.xml.
  • Makes filter configuration flexible without changing filter code.

Working of FilterConfig in Servlet Filter

Servlet Filter intercepts incoming HTTP requests and uses FilterConfig during initialization to process configuration details before forwarding the request to the appropriate resource such as a Servlet, JSP, or static file.

👁 Filter
ServletFilter
  • When the client sends an HTTP request, it first reaches the web container.
  • The web container initializes the filter and creates a FilterConfig object.
  • FilterConfig provides configuration details like init parameters and filter name to the filter.
  • The filter executes its logic (e.g., validation, logging, authentication) before request processing.
  • After processing, the filter forwards the request to the target resource (Servlet/JSP/static page).
  • The response is then sent back through the filter chain before reaching the client.

How to Add Filter Configuration in Servlet

There are two approaches to configure FilterConfig in a servlet application using the web.xml deployment descriptor and using annotations with @WebFilter.

1. Using web.xml (Traditional Approach)

In this approach, filter configuration and initialization parameters are defined inside the deployment descriptor using <filter> and <init-param> tags. The container creates the FilterConfig object automatically and passes it to the init() method.

  • Suitable for XML-based servlet applications.
  • Configuration can be changed without modifying source code.
  • Commonly used in older servlet applications.

Syntax:

<filter>
<filter-name>filterName</filter-name>
<filter-class>package.FilterClass</filter-class>
<init-param>
<param-name>paramName</param-name>
<param-value>value</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>filterName</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

2. Using @WebFilter Annotation (Annotation-Based Approach)

Servlet API also allows filter configuration using the @WebFilter annotation. Initialization parameters can be provided using the @WebInitParam annotation.

  • Removes the need for web.xml configuration.
  • Makes configuration simpler and code-centric.
  • Mostly used in modern servlet applications.

Syntax:

@WebFilter(
urlPatterns = "/*",
initParams = {

@WebInitParam(name = "paramName", value = "value")
}
)
public class FilterClass implements Filter {
}

Methods of FilterConfig Interface

The FilterConfig interface provides different methods that help a filter access its configuration details and servlet context information.

1. getFilterName()

This method returns the name of the filter defined in the web.xml file. It is mainly used to identify the current filter during execution.

Syntax:

public String getFilterName() 

2. getInitParameter(String name)

This method is used to get the value of a specific initialization parameter by passing its name. If the parameter is not available, it returns null.

Syntax:

public String getInitParameter(String name)

3. getInitParameterNames()

This method returns all initialization parameter names of the filter as an Enumeration object. It is useful when multiple parameters are configured.

Syntax:

public Enumeration getInitParameterNames()

4. getServletContext()

This method returns the ServletContext object associated with the web application. It allows the filter to access application-wide resources and information.

Syntax:

public ServletContext getServletContext() 

Example To Show How to Add FilterConfig in Servlet

Follow these steps to create Servlet Filter using FilterConfig, where we validate user credentials through initialization parameters defined in web.xml and control request flow using FilterChain.

Step 1: Create Dynamic Web Project

  • Open Eclipse IDE
  • Create a project: FilterConfigDemo
  • Select: Dynamic Web Project
  • Click Finish

Step 2: Create HTML File (index.html)

This is the front-end page where the user enters login credentials. The form sends data to the servlet via POST request.

Step 3: Create Servlet (abc.java)

This servlet processes the request after filter validation and displays a welcome message.

Step 4: Create Filter Class (filter_config_demo.java)

This filter intercepts the request before it reaches the servlet and validates username and password using FilterConfig parameters.

Step 5: Configure web.xml

This file defines servlet mapping, filter mapping, and init parameters required for FilterConfig.

Step 6: Run the Project

  • Right click project -> Run As -> Run on Server
  • Open browser:

http://localhost:8080/FilterConfigDemo/index.html

Output

Your Login page will appear on the browser

First, we entered the wrong user id and password

  • user id - admin
  • password - 12345
👁 Image

When incorrect credentials are provided, the filter handles the response by reloading the login page and displaying an error message.

👁 Image

Now we entered the Correct user id and password:

  • user id - sumit
  • password - sumit@123
👁 Image

When user provides correct credential then the servlet executes and generates response output

👁 Image

Explanation: When the user submits the login form, the request first passes through the Filter, where credentials are validated using FilterConfig parameters from web.xml. If valid, the request is forwarded to the servlet using chain.doFilter(); otherwise, the login page is reloaded with an error message.

Comment
Article Tags: