Servlet FilterConfig

Servlet FilterConfig is an interface which belongs to javax.servlet package.

FilterConfig is per filter created by web container.

Changes made in web.xml file will not require any change in the filter class.

FilterConfig interface having four main method to access details from web.xml

Below is the way to get FilterConfig object inside an Filter class.

Below is the way to declare init paramters per filter class.

To access above content we have below methods from FilterConfig interface.

getInitParameter(“country”)): This will return parameter value depending upon parameter name.

getInitParameterNames(); This will return all parameter name written inside <init-param> tag or define as an annotation on top of the class.

getServletContext(): This will return ServletConext Object.

getFilterName()): This will return Filter Name from <filter-name> tag mentioned in above screenshot.

Example

1. Create a LoginServlet.java class with the below content mentioned in below screenshot:

2. Inside same package com.javadoubts.servlet parallel to LoginServlet.java class, create one more LoginFilter.java class implementing Filter interface.

3. Create a web.xml with below content.

OUTPUT

Country = Singapore
param = country
Servlet Name = com.javadoubts.servlet.LoginFilter
doFilter() method called
doGet() method called

Imran Khan

Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.

  • Languages: Java, Python
  • Frameworks: J2EE, Spring, Struts 2.0, Hibernate
  • Web Technologies: React, HTML, CSS
  • Analytics: Adobe Analytics
  • Tools & Technologies: IntelliJ, JIRA

🌐 LinkedIn

📝 Blogs

📧 Imran Khan