❮ Previous Next ❯

FilterConfig Interface

We will get the configuration information detail from the web.xml file by using FilterConfig

object which is created by the web container.
Methods of FilterConfig interface

There are following 4 methods in the FilterConfig interface.

  1. public void init(FilterConfigconfig): init() method is invoked only once it is used to initialize the filter.
  2. public String getInitParameter(String parameterName): Returns the parameter value for the specified parameter name.
  3. publicjava.util.EnumerationgetInitParameterNames(): Returns an enumeration containing all the parameter names.
  4. publicServletContextgetServletContext(): Returns the ServletContext object.
Example of FilterConfig

In this example, if you change the param-value to no, request will be forwarded to the

servlet otherwise filter will create the response with the message: this page is

underprocessing. Let's see the simple example of FilterConfig.
Required Files:

  1. index.html
  2. MyFilter1.java
  3. WelcomeServlet.java
  4. web.xml

<a href="Myservlet">click</a>

import java.io.IOException;  
import java.io.PrintWriter;  
import javax.servlet.*;  
public class MyFilter1 implements Filter{  
FilterConfig config;  
public void init(FilterConfig config) throws ServletException {  
public void doFilter(ServletRequest req, ServletResponse resp,  
    FilterChain chain) throws IOException, ServletException {  
    PrintWriter out=resp.getWriter();  
    String s1=config.getInitParameter("processing");  
         out.print("This page is under processing");  
         chain.doFilter(req, resp);//sends request to next resource  
public void destroy() {}  

import java.io.IOException;  
import java.io.PrintWriter;  
import javax.servlet.ServletException;  
import javax.servlet.http.*;  
public class WelcomeServlet extends HttpServlet {  
public void doGet(HttpServletRequest request, HttpServletResponse response)  
        throws ServletException, IOException {  
        PrintWriter out = response.getWriter();  
        out.print("welcome to JavaRace");  


❮ Previous Next ❯

Follow Us


Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Email: info@silantechnology.com
      Phone: 0674-2361252
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved