开发者

Tomcat filter generates duplicate localhost.log lines

This code, most of which I inherited, runs fine except System.out.println("Success") generates a lot (7-37, random) of identical lines in localhost.log instead of just one when it runs:

Mar 1, 2011 8:49:47 AM org.apache.catalina.core.StandardWrapperValve invoke
Success
Mar 1, 2011 8:49:47 AM org.apache.catalina.core.StandardWrapperValve invoke
Success
Mar 1, 2011 8:49:47 AM org.apache.catalina.core.StandardWrapperValve invoke
Success

What's going on??

public class SpecialFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain fc)
        throws IOException, ServletException {
    if (request instanceof HttpServletRequest) {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        String mainID = httpRequest.getRemoteUser();
        String username = "";
        try {
            Cookie c[] = httpRequest.getCookies();
            if (c == null) {
                username = getID(mainID);  // method omitted, just executes a SQL query
            } else {
                boolean cookieFound = false;
                for (int i = 0; i < c.length; i++) {
                    if (c[i].getName().equals("mainCookie")) {
                        username = c[i].getValue();
                        cookieFound = true;
                        break;
   开发者_如何转开发                 }
                }
                if (cookieFound) {
                    System.out.println("Success");
                } else {
                    username = getID(mainID);
                }
            }
        } catch (SQLException e) {
            System.out.println("Error 1 " + e);
            throw new ServletException(error, e);
        }
        AuthRequestWrapper wrapper = new AuthRequestWrapper(httpRequest, username);
        fc.doFilter(wrapper, response);
    }
    else {
        throw new RuntimeException("request is not a valid httpRequest object.");
    }
}

}


With log4j, I've encountered duplicate log messages when I assign overlapping paths to an appender. For example

com.blah.blam=appendThis
com.blah=appendThis


You didn't supply the URL mapping for this filter, but I suspect that it's mapped to other elements in the generated HTML.

For example, if a page contains 5 images and 2 CSS files the client would make 7 additional HTTP requests after it parses the generated HTML for a single page request. In this case I would expect to see 8 SUCCESS lines printed.

You can always see the path for each request by printing httpRequest.getRequestURL()


You have multiple logger attached to the same class. Just disable your root logger. That is the problem with duplicate log statements most of the time. cheers

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜