FilterConfig object is created and used by the web container to pass init parameters to a filter during initialization.
Methods of FilterConfig interface:
1. getFilterName(): Returns the name of the filter defined in web.xml.
Syntax:
public String getFilterName()
2. getInitParameter(String name): Returns the value of the specified parameter.
Syntax:
public String getInitParameter(String name)
3. getInitParameterNames(): Returns the names of all parameters as Enumeration.
Syntax:
public Enumeration getInitParameterNames()
4. getServletContext(): Returns the object of ServletContext.
Syntax:
public ServletContext getServletContext()
Example:
MyFilter.java
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; /** * This class is used as a filter. * @author W3schools360 */ public class MyFilter implements Filter { private static final long serialVersionUID = 1L; private FilterConfig filterConfig; public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; } public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); //get parameters from filterConfig object. String appUser = filterConfig.getInitParameter("appUser"); if(appUser.equals("jai")){ chain.doFilter(request, response); } else { out.print("Invalid application user."); } } public void destroy() { } }
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * This class is used to print login * message if user logged in successfully. */ public class WelcomeServlet extends HttpServlet { private static final long serialVersionUID = 1L; //no-argument constructor public WelcomeServlet() { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.print("<h1>Valid application user.</h1>"); out.close(); } }
web.xml
<!--?xml version="1.0" encoding="UTF-8"?--> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <servlet> <servlet-name>WelcomeServlet</servlet-name> <servlet-class> com.w3schools.business.WelcomeServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>WelcomeServlet</servlet-name> <url-pattern>/WelcomeServlet</url-pattern> </servlet-mapping> <filter> <filter-name>MyFilter</filter-name> <filter-class> com.w3schools.business.MyFilter </filter-class> <init-param> <param-name>appUser</param-name> <param-value>jai</param-value> </init-param> </filter> <filter-mapping> <filter-name>MyFilter</filter-name> <url-pattern>/WelcomeServlet</url-pattern> </filter-mapping> </web-app>