For example, Tomcat stores the log files in the tomcat_dir/logs directory. The problem
is that the doFilter method executes before the servlet or JSP page with
which it is associated, so you don??™t have access to the servlet instance and thus can??™t
call the log methods that are inherited from GenericServlet. Furthermore, the
Filter API provides no simple way to access the ServletContext from the
doFilter method. The only filter-related class that has a method to access the
ServletContext is FilterConfig with its getServletContext method. A
FilterConfig object is passed to the init method but is not automatically stored
in a location that is available to doFilter.
So, you have to store the FilterConfig yourself. Simply create a field (instance
variable) of type FilterConfig, then override init to assign its argument to that
field. Because you typically use the FilterConfig object only to access the ServletContext
and the filter name, you can store the ServletContext and name in
fields as well. Here is a simple example:
public class SomeFilter implements Filter {
protected FilterConfig config;
private ServletContext context;
private String filterName;
public void init(FilterConfig config)
throws ServletException {
this.config = config; // In case it is needed by subclass.
context = config.getServletContext();
filterName = config.getFilterName();
}
// doFilter and destroy methods.
Pages:
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267