Java by API/javax.servlet/FilterChain

Материал из Java эксперт
Перейти к: навигация, поиск

FilterChain: doFilter(ServletRequest arg0, ServletResponse arg1)

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class LogFilter implements Filter {
  FilterConfig config;
  public void setFilterConfig(FilterConfig config) {
    this.config = config;
  }
  public FilterConfig getFilterConfig() {
    return config;
  }
  public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) {
    ServletContext context = getFilterConfig().getServletContext();
    long bef = System.currentTimeMillis();
    try {
      chain.doFilter(req, res);
    } catch (Exception e) {
    } 
    long aft = System.currentTimeMillis();
    context.log("Request to " + req.getRemoteHost() + ": " + (aft - bef));
  }
  public void init(FilterConfig arg0) throws ServletException {
  }
  public void destroy() {
  }
}