Joh*_*ohn 7 java inputstream tomcat6 servlet-filters
我在我的应用程序中添加了一个过滤器,它只是记录了有关请求的某些内容.我的一些servlet从中读取ServletRequest#getInputStream.自添加此过滤器以来,那些读取的servlet ServletRequest#getInputStream不再用作输入流是空的.只需从我的注释中禁用过滤器即可web.xml解决问题.
为什么会发生这种情况,是否有办法使用过滤器而不会弄乱ServletRequest#getInputStream?
过滤器实际上是Tomcat RequestDumperFilter包含在其示例Web应用程序中的一个.我只会包含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);
}
Run Code Online (Sandbox Code Playgroud)
从我通过Google搜索读到的内容,getInputStream如果先调用,以下任何方法都将呈现为空:
getParametergetParameterNamesgetParameterValuesgetParameterMap感谢SimoneGianni指出我正确的方向:
以下是一些消息来源
这个人实际上有一个类似的问题,并创建了自己的包装类作为解决方法.
如果调用 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 请求将参数编码为请求正文,因此要读取参数,您实际上必须使用(容器为您执行此操作)输入流。
| 归档时间: |
|
| 查看次数: |
3229 次 |
| 最近记录: |