LogFilter.java Source code

Java tutorial

Introduction

Here is the source code for LogFilter.java

Source

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() {
    }
}