Tomcat过滤器对请求输入流产生负面影响(输入流为空)

时间:2011-07-21 18:03:29

标签: java tomcat6 inputstream servlet-filters

我在我的应用程序中添加了一个过滤器,它只记录有关请求的某些内容。我的一些servlet从ServletRequest#getInputStream读取。自添加此过滤器以来,那些从ServletRequest#getInputStream读取的servlet不再用作输入流是空的。只需在我的web.xml中对其进行评论即可停用该过滤器。

为什么会发生这种情况,是否有办法使用过滤器而不会弄乱ServletRequest#getInputStream

过滤器实际上是其示例网络应用程序中包含的Tomcat RequestDumperFilter。我只会包含doFilter方法,因为这是重要的部分。如果你想看到整件事,我就把它放在PasteBin上。

/**
 * Time the processing that is performed by all subsequent filters in the
 * current filter stack, including the ultimately invoked servlet.
 *
 * @param request The servlet request we are processing
 * @param result The servlet response we are creating
 * @param chain The filter chain we are processing
 *
 * @exception IOException if an input/output error occurs
 * @exception ServletException if a servlet error occurs
 */
public void doFilter(ServletRequest request, ServletResponse response,
                     FilterChain chain)
throws IOException, ServletException {

    if (filterConfig == null)
    return;

// Render the generic servlet request properties
StringWriter sw = new StringWriter();
PrintWriter writer = new PrintWriter(sw);
writer.println("Request Received at " +
           (new Timestamp(System.currentTimeMillis())));
writer.println(" characterEncoding=" + request.getCharacterEncoding());
writer.println("     contentLength=" + request.getContentLength());
writer.println("       contentType=" + request.getContentType());
writer.println("            locale=" + request.getLocale());
writer.print("           locales=");
Enumeration locales = request.getLocales();
boolean first = true;
while (locales.hasMoreElements()) {
    Locale locale = (Locale) locales.nextElement();
    if (first)
        first = false;
    else
        writer.print(", ");
    writer.print(locale.toString());
}
writer.println();
Enumeration names = request.getParameterNames();
while (names.hasMoreElements()) {
    String name = (String) names.nextElement();
    writer.print("         parameter=" + name + "=");
    String values[] = request.getParameterValues(name);
    for (int i = 0; i < values.length; i++) {
        if (i > 0)
        writer.print(", ");
    writer.print(values[i]);
    }
    writer.println();
}
writer.println("          protocol=" + request.getProtocol());
writer.println("        remoteAddr=" + request.getRemoteAddr());
writer.println("        remoteHost=" + request.getRemoteHost());
writer.println("            scheme=" + request.getScheme());
writer.println("        serverName=" + request.getServerName());
writer.println("        serverPort=" + request.getServerPort());
writer.println("          isSecure=" + request.isSecure());

// Render the HTTP servlet request properties
if (request instanceof HttpServletRequest) {
    writer.println("---------------------------------------------");
    HttpServletRequest hrequest = (HttpServletRequest) request;
    writer.println("       contextPath=" + hrequest.getContextPath());
    Cookie cookies[] = hrequest.getCookies();
        if (cookies == null)
            cookies = new Cookie[0];
    for (int i = 0; i < cookies.length; i++) {
        writer.println("            cookie=" + cookies[i].getName() +
               "=" + cookies[i].getValue());
    }
    names = hrequest.getHeaderNames();
    while (names.hasMoreElements()) {
        String name = (String) names.nextElement();
    String value = hrequest.getHeader(name);
        writer.println("            header=" + name + "=" + value);
    }
    writer.println("            method=" + hrequest.getMethod());
    writer.println("          pathInfo=" + hrequest.getPathInfo());
    writer.println("       queryString=" + hrequest.getQueryString());
    writer.println("        remoteUser=" + hrequest.getRemoteUser());
    writer.println("requestedSessionId=" +
           hrequest.getRequestedSessionId());
    writer.println("        requestURI=" + hrequest.getRequestURI());
    writer.println("       servletPath=" + hrequest.getServletPath());
}
writer.println("=============================================");

// Log the resulting string
writer.flush();
filterConfig.getServletContext().log(sw.getBuffer().toString());

// Pass control on to the next filter
    chain.doFilter(request, response);

}

结论

根据我Googling所读到的内容,如果首先调用getInputStream,则以下任何一种方法都会呈现为空:

  • getParameter
  • getParameterNames
  • getParameterValues
  • getParameterMap

感谢SimoneGianni指出我正确的方向:

以下是一些消息来源

这个人实际上有一个类似的问题,并创建了自己的包装类作为解决方法。

2 个答案:

答案 0 :(得分:3)

如果你调用getParameters,getParameterNames和类似的方法,你可能会干扰getInputStream或getReader。这在servlet文档中没有明确说明,但在官方servlet javadocs中有一些相反的警告(getInputStream干扰getParameter)(自1.3起,见http://download.oracle.com/javaee/1.3/api/javax/servlet/ServletRequest.html#getParameter(java.lang.String))

你在POST上看到这个问题了吗?由于POST请求将参数编码为请求主体,因此要读取实际必须使用的参数(容器为您执行此操作)输入流。

答案 1 :(得分:-1)

哇。这是一个很好的。我打赌RequestDumper必须解析整个InputStream,以及转储请求,这就是InputStream为空的原因。我想你不能在任何计划进行getInputStream的Servlet前面RequestDumper。不过,从这一点来看,我对你的选择感到茫然。也许直接从HttpRequest转储你感兴趣的参数。在chain.doFilter(request,response)之后;调用