TimerFilter : A Simple HTTP Servlet Filter


import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class TimerFilter implements Filter {

	public void destroy() {
		System.out.println("destroy");
	}

	public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException {
		System.out.println("doFilter");
		long startTime = System.currentTimeMillis();
		filterChain.doFilter(req, res);
		long stopTime = System.currentTimeMillis();
		System.out.println("took " + (stopTime-startTime) + " milli seconds.");

	}

	public void init(FilterConfig arg0) throws ServletException {
		System.out.println("init");
	}

}

<web-app>

    <filter>
        <filter-name>Timer</filter-name>
        <filter-class>com.msa.filter.TimerFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>Timer</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>    

</web-app>


You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

AddThis Social Bookmark Button

Similar Posts

Comments are closed.