Wednesday, 30 September 2015

FilterConfig

FilterConfig

An object of FilterConfig is created by the web container. This object can be used to get the configuration information from the web.xml file.

Methods of FilterConfig interface

There are following 4 methods in the FilterConfig interface.
  1. public void init(FilterConfig config): 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. public java.util.Enumeration getInitParameterNames(): Returns an enumeration containing all the parameter names.
  4. public ServletContext getServletContext(): 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. Here, we have created 4 files:
  • index.html
  • MyFilter.java
  • HelloServlet.java
  • web.xml
index.html
  1. <a href="servlet1">click here</a>  

MyFilter.java
  1. import java.io.IOException;  
  2. import java.io.PrintWriter;  
  3.   
  4. import javax.servlet.*;  
  5.   
  6. public class MyFilter implements Filter{  
  7. FilterConfig config;  
  8.   
  9. public void init(FilterConfig config) throws ServletException {  
  10.     this.config=config;  
  11. }  
  12.   
  13. public void doFilter(ServletRequest req, ServletResponse resp,  
  14.     FilterChain chain) throws IOException, ServletException {  
  15.       
  16.     PrintWriter out=resp.getWriter();  
  17.           
  18.     String s=config.getInitParameter("construction");  
  19.           
  20.     if(s.equals("yes")){  
  21.          out.print("This page is under construction");  
  22.     }  
  23.     else{  
  24.          chain.doFilter(req, resp);//sends request to next resource  
  25.     }  
  26.           
  27. }  
  28. public void destroy() {}  
  29. }  

HelloServlet.java
  1. import java.io.IOException;  
  2. import java.io.PrintWriter;  
  3.   
  4. import javax.servlet.ServletException;  
  5. import javax.servlet.http.*;  
  6.   
  7. public class HelloServlet extends HttpServlet {  
  8. public void doGet(HttpServletRequest request, HttpServletResponse response)  
  9.         throws ServletException, IOException {  
  10.   
  11.         response.setContentType("text/html");  
  12.         PrintWriter out = response.getWriter();  
  13.       
  14.         out.print("<br>welcome to servlet<br>");  
  15.           
  16.     }  
  17.   
  18. }  

web.xml
  1. <web-app>  
  2.   
  3.  <servlet>  
  4.     <servlet-name>HelloServlet</servlet-name>  
  5.     <servlet-class>HelloServlet</servlet-class>  
  6.   </servlet>  
  7.   
  8.   <servlet-mapping>  
  9.     <servlet-name>HelloServlet</servlet-name>  
  10.     <url-pattern>/servlet1</url-pattern>  
  11.   </servlet-mapping>  
  12.     
  13.   <filter>  
  14.   <filter-name>f1</filter-name>  
  15.   <filter-class>MyFilter</filter-class>  
  16.   <init-param>  
  17.   <param-name>construction</param-name>  
  18.   <param-value>no</param-value>  
  19.   </init-param>  
  20.   </filter>  
  21.   <filter-mapping>  
  22.   <filter-name>f1</filter-name>  
  23.   <url-pattern>/servlet1</url-pattern>  
  24.   </filter-mapping>  
  25.     
  26.     
  27. </web-app>  

No comments:

Post a Comment