Skip to content

Instantly share code, notes, and snippets.

@diverted247
Created November 14, 2015 14:45
Show Gist options
  • Save diverted247/e25d4b1a077b739a5473 to your computer and use it in GitHub Desktop.
Save diverted247/e25d4b1a077b739a5473 to your computer and use it in GitHub Desktop.
Servlet Filter
// Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
// Implements Filter class
public class LogFilter implements Filter {
public void init(FilterConfig config)
throws ServletException{
// Get init parameter
String testParam = config.getInitParameter("test-param");
//Print the init parameter
System.out.println("Test Param: " + testParam);
}
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws java.io.IOException, ServletException {
// Get the IP address of client machine.
String ipAddress = request.getRemoteAddr();
// Log the IP address and current timestamp.
System.out.println("IP "+ ipAddress + ", Time "
+ new Date().toString());
// Pass request back down the filter chain
chain.doFilter(request,response);
}
public void destroy( ){
/* Called before the Filter instance is removed
from service by the web container*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment