How to use filters in JSP and Servlets for logging information in ServletContext logs ?

A simple web application demonstrating the usage of filters in JSP and Servlets. This application uses a LoggingTimeFilter for logging the time of incoming request and outgoing response in ServletContext's log . This information gets printed on the Server's log displayed on the console.

Filter Used here is :


package com.hubberspot.filter;

import java.io.IOException;
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;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

// Any request accessed by pattern /* will be intercepted by this
// filter whose name is LoggingTimeFilter. No web.xml is needed 
// if we are using annotations
@WebFilter(urlPatterns={"/*"} , filterName="LoggingTimeFilter" )
public class LoggingTimeFilter implements Filter {
 // This filter implements Filter so it has to provide implementations
 // for Filter interface 

 private FilterConfig filterConfig = null;

 public FilterConfig getFilterConfig() {
  return filterConfig;
 }

 public void setFilterConfig(FilterConfig fConfig) {
  this.filterConfig = fConfig;
 }

 // 3rd implementation of Filter interface
 public void destroy() {
  this.filterConfig = null;
 }

 // 2nd implementation of Filter interface 
 public void doFilter(
   ServletRequest request, 
   ServletResponse response, 
   FilterChain chain
   ) throws IOException, ServletException {

  // extracting the ServletContext from filterConfig
  ServletContext context = filterConfig.getServletContext();
  // Taking incoming time of request
  double incomingTime = System.currentTimeMillis();

  // printing the information in the ServletContext's log
  context.log("Incoming request time : "+ incomingTime/1000.0);

  // Pre processing of filter before doFilter() method
  chain.doFilter(request, response); // here request goes to server
  // Post processing of filter before doFilter() method  

  // Taking outgoing time of response
  double outgoingTime = System.currentTimeMillis();

  // printing the information in the ServletContext's log  
  context.log("Outgoing response time : "+ outgoingTime/1000.0);


  double diffInTime = outgoingTime - incomingTime ; 
  // printing the information in the ServletContext's log
  context.log("Difference in the request and response : "
    + diffInTime/1000.0);
 }

 // 3rd implementation of Filter interface
 public void init(FilterConfig fConfig) throws ServletException {
  this.filterConfig = fConfig;
 }
}




After implementing filter, just make a request to a server with any implemented Url of JSP or Servlet and see the output at the console. I made a anonymous request to server and got the following output at the server's log console: 


 



Video tutorial to demonstrate @WebFilter annotation. 





Using annotation