An object of FilterConfig is created by the web container for each filter. It can be used to read filter init parameters. Placed in web.xml file. If the configuration information is modified from the web.xml file, we don’t need to change the filter. So it is easier to manage the web application if any specific content is modified from time to time.
Methods of FilterConfig Interface
There are four methods in the FilterConfig interface.
1. String getFilterName(): Return the filter-name of this filter as defined in the deployment descriptor.
Syntax: public String getFilterName()
2. String getInitParameter(String name): Return a String containing the value of the named initialization parameter, or null if the parameter does not exist.
Syntax: public String getInitParameter(String name)
3. Enumeration getInitParameterNames(): Return the names of the filter’s initialization parameter as an Enumeration of String object’s or an empty Enumeration if the filter has no initialization parameters.
Syntax: public Enumeration getInitParameterNames()
4. ServletContext getServletContext(): Return a reference to the ServletContext in which caller is executing
Syntax: public ServletContext getServletContext()
Example
index.html
HTML
< html > < head > < title >TODO supply a title</ title > < meta charset = "UTF-8" > < meta name = "viewport" content = "width=device-width, initial-scale=1.0" > </ head > < body > < h2 >FilterConfig Demo</ h2 > < form action = "abc" method = "post" > User name < input type = "text" name = "un" >< br > Password < input type = "password" name = "up" >< br > < input type = "submit" value = "Login" >< input type = "reset" value = "Clear" > </ form > </ body > </ html > |
abc.java
Java
import static java.lang.System.out; 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; public class abc extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType( "text/html;charset=UTF-8" ); PrintWriter p = response.getWriter(); p.println( "<font color='red'><h2>Welcome User</h2></font>" ); } // <editor-fold defaultstate="collapsed" // desc="HttpServlet methods. Click on the + sign on the // left to edit the code."> @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } @Override public String getServletInfo() { return "Short description" ; } } |
filter_config_demo.java
Java
package demo; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class filter_config_demo implements Filter { FilterConfig fc; @Override public void init(FilterConfig filterConfig) throws ServletException { this .fc = filterConfig; } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { String un, up; un = request.getParameter( "un" ); up = request.getParameter( "up" ); PrintWriter p = response.getWriter(); System.out.println( "My filter Name is " + fc.getFilterName()); if (un.equals(fc.getInitParameter( "username" )) && up.equals( fc.getInitParameter( "userpassword" ))) { chain.doFilter(request, response); } else { RequestDispatcher rd = request.getRequestDispatcher( "/index.html" ); rd.include(request, response); p.println( "incorrect user name and password" ); } } @Override public void destroy() { this .fc = null ; } } |
web.xml
XML
<? xml version = "1.0" encoding = "UTF-8" ?> xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee < filter > < filter-name >filter1</ filter-name > < filter-class >demo.filter_config_demo</ filter-class > < init-param > < param-name >username</ param-name > < param-value >sumit</ param-value > </ init-param > < init-param > < param-name >userpassword</ param-name > < param-value >sumit@123</ param-value > </ init-param > </ filter > < filter-mapping > < filter-name >filter1</ filter-name > < servlet-name >abc</ servlet-name > </ filter-mapping > < servlet > < servlet-name >abc</ servlet-name > < servlet-class >abc</ servlet-class > </ servlet > < servlet-mapping > < servlet-name >abc</ servlet-name > < url-pattern >/abc</ url-pattern > </ servlet-mapping > < session-config > < session-timeout >30</ session-timeout > </ session-config > </ web-app > |
Output
First, we entered the wrong user id and password:
- user id – admin
- password – 12345
Now we entered the Correct user id and password:
- user id – sumit
- password – sumit@123