SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 261 | Next

Larry Brown, Marty Hall, and Yaakov Chaikin

"Core Servlets and JavaServer Pages, Volume 2"

SomeFilterClass

param1
value1


param2
value2


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.
5.6 Example: An Access Time Filter 223
5.6 Example: An Access Time Filter
The LogFilter of Section 5.


Pages:
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273