Recipe 19.5 Configuring Initialization Parameters for a Filter


Problem

You want to make an initialization (init) parameter available to a filter .

Solution

Use the init-param child element of the filter element to declare the initialization parameter and its value. Inside the filter, access the init parameter by calling the FilerConfig object's getInitParameter method.

Discussion

Example 19-6 shows a filter declared in the deployment descriptor. The filter includes an init parameter named log-id .

Example 19-6. A filter declared in the deployment descriptor with an init parameter
 <filter>     <filter-name>LogFilter</filter-name>     <filter-class>com.jspservletcookbook.LogFilter</filter-class>  <init-param>         <param-name>log-id</param-name>         <param-value>A102003</param-value>     </init-param>  </filter> 

Example 19-7 shows the code you would use inside the filter to access the init parameter and its value. The code initializes the FilterConfig object in its init method, which is called once when the web container creates an instance of the filter. The code then gets the value of the filter's init parameter by calling:

 String id = config.getInitParameter("log-id"); 

Make sure that the code checks whether the return value from getInitParameter is null before the code does something with that object.

Example 19-7. Accessing an init param value in a filter
 package com.jspservletcookbook; import javax.servlet.*; import javax.servlet.http.*; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; public class LogFilter implements Filter {  private FilterConfig config;  private Logger log;          // Creates new LogFilter     public LogFilter( ) {}  public void  init(FilterConfig filterConfig) throws ServletException{          this.config = filterConfig;  //load the configuration for this application's loggers      //using the servletLog.properties file     PropertyConfigurator.configure(config.getServletContext( ).        getRealPath("/") +            "WEB-INF/classes/servletLog.properties");         log = Logger.getLogger(LogFilter.class);         log.info("Logger instantiated in "+ getClass( ).getName( ));     }          public void  doFilter(ServletRequest request, ServletResponse response,         FilterChain chain) throws java.io.IOException, ServletException {       HttpServletRequest req = null;  String id = config.getInitParameter("log-id");          if (id == null)         id = "unknown";           if (log != null && (request instanceof HttpServletRequest)){                  req = (HttpServletRequest) request;         log.info("Log id:" + id + ": Request received from: " +             req.getRemoteHost( ) + " for " + req.getRequestURL( )); }         chain.doFilter(request,response);  }// doFilter          public void destroy( ){         /*called before the Filter instance is removed          from service by the web container*/         log = null;     } } 

Here is how the log output appears:

 INFO - Log id:A102003: Request received from: localhost for http://localhost:8080/ home/requestheaders 

You can also use the FilterConfig object's getInitParameterNames method to get all of the init parameter names in a java.util.Enumeration object.


See Also

Recipe 7.9 on using a filter to read request parameter values; Recipe 11.11 on using a filter to monitor session attributes; Recipe 18.3 on using a filter to alter then forward the request; Recipe 19.1-Recipe 19.4 on mapping filters to web components ; Recipe 19.6 on blocking a request; Recipe 19.7 on filtering the HttpServletResponse ; Recipe 19.8 on using filters with RequestDispatchers ; Recipe 19.9 on using filters to check request parameters; Recipe 19.10 on using filters to disallow requests from certain IP addresses.



Java Servlet & JSP Cookbook
Java Servlet & JSP Cookbook
ISBN: 0596005725
EAN: 2147483647
Year: 2004
Pages: 326

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net