Servlet FilterConfig
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
- 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, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.