The following example shows how to implement a filter:
package com.ibm.websphere;
import java.io.*;
import javax.servlet.*;
public class LoggingFilter implements Filter
{
File _loggingFile = null;
// implement the required init method public void init(FilterConfig fc)
{
// create the logging file xxx;
}
// implement the required doFilter method...this is where most of
the work is done public void doFilter(ServletRequest request,
ServletResponse response, FilterChain chain)
{
try {
// add request info to the log file synchronized(_loggingFile)
{
xxx;
}
// pass the request on to the next resource in the chain chain.doFilter(request, response);
}
catch (Throwable t)
{
// handle problem...
}
}
// implement the required destroy method public void destroy()
{
// make sure logging file is closed _loggingFile.close();
}
}
Related concepts
Servlet filtering
Related reference
Filter, FilterChain, FilterConfig classes for servlet filtering
Web applications: Resources for learning