In this article, we will see how to use filters and logging filters in Java web programming.
With filters allows us control the request before it reach to its resource.
Filters can have many different tasks, as logging is one of the useful one. We can create filter class by implementing javax.servlet.Filter,
Lets see it in an example:
package com.prj23m.filters;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
public class LoggingFilters implements Filter
{
public void init(FilterConfig filterConfig) throws ServletException
{
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException
{
System.out.println("webfilter1: dofilter has been called..");
HttpServletRequest r = (HttpServletRequest) servletRequest;
System.out.println(r.getRequestURL());
}
public void destroy()
{
}
}
Lets see it in a java example.
In above example, we created our logging filter and implemented necessary definition to our web.xml file. The requests hit to relevant, they land as a log entry as output indicates.