Web app. doesn't display any content after I set servlet TrackingFilter

36 views Asked by At

I created a simple webapp using tomcat to test the tracking filter. The problem is the webapp works fine without tracking filter. However when I implement tracking filter, the webapp doesn't display any content. I am able to see the tracking information on eclipse's console, however nothing on the browser.

My tracking Filter Code:

package com.myapp.filters;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Date;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class TrackingFilter implements Filter{

private FilterConfig config =null;

@Override
public void destroy() {
    // TODO Auto-generated method stub

}

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest httpReq = (HttpServletRequest)req;
    /*
     * Date of website entering
     */
    Date now = new Date();
    String host = httpReq.getHeader("host");
    String ip = InetAddress.getLocalHost().getHostAddress();

    config.getServletContext().log("TrackingFilter. " + "Date: " + now + ",  " + "host: " + host + ", " + "IP: " + ip);


    System.out.println("Exiting tracking filtering");

}

@Override
public void init(FilterConfig config) throws ServletException {
    this.config = config;

}
}

my web.xml configuration

<!-- Tracking Filter -->
<filter>
    <filter-name>Tracking Filter</filter-name>
    <filter-class>com.myapp.filters.TrackingFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>Tracking Filter</filter-name>
    <url-pattern>/*</url-pattern>   
</filter-mapping>
1

There are 1 answers

0
JB Nizet On BEST ANSWER

That's because the filter breaks the chain, and does not propagate the request to the next filters and servlets. It should contain

chain.doFilter(req, res);