After the Web application is deployed on an external server and the logging filter
is attached, a client request for the Web application home page results in an entry in
the log file like ???audits.irs.gov tried to access http://www.filtersrus.com/filters/
index.jsp on Fri Apr 14 15:16:15 EDT 2001. (Reported by Logger.)??? On Tomcat, the
log file is located in the tomcat_dir/logs directory. For example, Listing 5.9 shows
partial contents of the localhost.2006-04-14.log file.
5.4 Example: A Logging Filter 219
Listing 5.7 LogFilter.java
package coreservlets.filters;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
/** Simple filter that prints a report in the log file
* whenever the associated servlets or JSP pages
* are accessed.
*/
public class LogFilter implements Filter {
protected FilterConfig config;
private ServletContext context;
private String filterName;
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws ServletException, IOException {
HttpServletRequest req = (HttpServletRequest)request;
context.log(req.getRemoteHost() +
" tried to access " +
req.getRequestURL() +
" on " + new Date() + ". " +
"(Reported by " + filterName + ".)");
chain.doFilter(request,response);
}
public void init(FilterConfig config)
throws ServletException {
this.config = config; // In case it is needed by subclass.
Pages:
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269