9.5. Using Filter Initialization Parameters

With servlets and JSP pages, you can customize the initialization behavior by supply initialization parameters. For details, see Section 5.5 (Initializing and Preloading Servlets and JSP Pages). The reason this capability is useful is that there are three distinct groups that might want to customize the behavior of servlets or JSP pages:

  1. Developers. They customize the behavior by changing the code of the servlet or JSP page itself.

  2. End users. They customize the behavior by entering values in HTML forms.

  3. Deployers. This third group is the one served by initialization parameters. Members of this group are people who take existing Web applications (or individual servlets or JSP pages) and deploy them in a customized environment. They are not necessarily developers, so it is not realistic to expect them to modify the servlet and JSP code. Besides, you often omit the source code when distributing servlets. So, developers need a standard way to allow deployers to change servlet and JSP behavior.

If these capabilities are useful for servlets and JSP pages, you would expect them to also be useful for the filters that apply to servlets and JSP page. Indeed they are. However, since filters execute before the servlets or JSP pages to which they are attached, it is not normally possible for end users to customize filter behavior. Nevertheless, it is still useful to permit deployers (not just developers) to customize filter behavior by providing initialization parameters. This behavior is accomplished with the following steps.

1.
Define initialization parameters. Use the init-param subelement of filter in web.xml along with param-name and param-value subelements, as follows.

<filter> 
  <filter-name>SomeFilter</filter-name> 
  <filter-class>somePackage.SomeFilterClass</filter-class> 
  <init-param>
								<param-name>param1</param-name>
								<param-value>value1</param-value>
								</init-param>
								<init-param>
								<param-name>param2</param-name>
								<param-value>value2</param-value>
								</init-param> 
</filter> 

2.
Read the initialization parameters. Call the getInitParameter method of FilterConfig from the init method of your filter, as follows.

public void init(FilterConfig config) 
    throws ServletException {
  String val1 = config.getInitParameter("param1"); 
  String val2 = config.getInitParameter("param2"); 
  ... 
} 

3.
Parse the initialization parameters. Like servlet and JSP initialization parameters, each filter initialization value is of type String. So, if you want a value of another type, you have to convert it yourself. For example, you would use Integer.parseInt to turn the String "7" into the int 7. When parsing, don’t forget to check for missing and malformed data. Missing initialization parameters result in null being returned from getInitParameter. Even if the parameters exist, you should consider the possibility that the deployer formatted the value improperly. For example, when converting a String to an int, you should enclose the Integer.parseInt call within a try/catch block that catches NumberFormatException. This handles null and incorrectly formatted values in one fell swoop.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.189.228