Created
November 14, 2015 14:45
-
-
Save diverted247/e25d4b1a077b739a5473 to your computer and use it in GitHub Desktop.
Servlet Filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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