logo

FilterConfig


Show

An item of FilterConfig is created via means of the web container. This item may be used to get the configuration records from the web.xml file.

Methods of FilterConfig interface

There are following four strategies withinside the FilterConfig interface.

  1. public void init(FilterConfig config): init() approach is invoked most effectively as soon as it's far used to initialize the filter.
  2. public String getInitParameter(String parameterName): Returns the parameter fee for the required parameter name.
  3. public java.util.Enumeration getInitParameterNames(): Returns an enumeration containing all of the parameter names.
  4. public ServletContext getServletContext(): Returns the ServletContext item.

Example of FilterConfig

In this instance, in case you alternate the param-fee to no, the request can be forwarded to the servlet in any other case, the filter will create the reaction with the message: this web page is under processing. Let's see the easy instance of FilterConfig. Here, we've got created four files:

  • index.html
  • MyFilter.java
  • HelloServlet.java
  • web.xml

Index.html

<a href="servlet1">click here</a>

Myfilter.java

import java.io.IOException;  
import java.io.PrintWriter;    

import javax.servlet.*;   

public class MyFilter implements Filter{  
FilterConfig config;  

public void init(FilterConfig config) throws ServletException {  
    this.config=config;  
}    

public void doFilter(ServletRequest req, ServletResponse resp,  
    FilterChain chain) throws IOException, ServletException {        

    PrintWriter out=resp.getWriter();            

    String s=config.getInitParameter("construction");            

    if(s.equals("yes")){  
         out.print("This page is under construction");  
    }  
    else{  
         chain.doFilter(req, resp);//sends request to next resource  
    }            

}  
public void destroy() {}  

}  

HelloServlet.java

import java.io.IOException;  
import java.io.PrintWriter;    

import javax.servlet.ServletException;  
import javax.servlet.http.*;    

public class HelloServlet extends HttpServlet {  
public void doGet(HttpServletRequest request, HttpServletResponse response)  
       throws ServletException, IOException {   

        response.setContentType("text/html");  
        PrintWriter out = response.getWriter();       

        out.print("
welcome to servlet
"
); } }

Web.xml

<web-app>  

 <servlet>  
    <servlet-name>HelloServlet</servlet-name>  
    <servlet-class>HelloServlet</servlet-class>  
  </servlet>  
  
  <servlet-mapping>  
    <servlet-name>HelloServlet</servlet-name>  
    <url-pattern>/servlet1</url-pattern>  
  </servlet-mapping>    

  <filter>  
  <filter-name>f1</filter-name>  
  <filter-class>MyFilter</filter-class>  
  <init-param>  
  <param-name>construction</param-name>  
  <param-value>no</param-value>  
  </init-param>  
  </filter>  

  <filter-mapping>  
  <filter-name>f1</filter-name>  
  <url-pattern>/servlet1</url-pattern>  
  </filter-mapping>     

</web-app>  

download this example (developed using Myeclipse IDE)

download this example (developed using Eclipse IDE)

download this example (developed using Netbeans IDE)