嗨,我正在使用HttpServletRequest,并尝试设置头集合。
下面是代码:
public static Map<String, String> getHeaders(HttpServletRequest request) {
Map<String, String> headers = new HashMap<String, String>();
Enumeration<String> headerNames = request.getHeaderNames();
if (headerNames != null) {
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
String header = request.getHeader(headerName);
headers.put(headerName, header);
}
}
return headers;
}
此方法似乎在headernames. nextElement()处抛出了Null指针异常。
是否有可能hasMoreElements检查返回true,但元素headerNames. nextElement反过来导致空指针异常?
堆栈跟踪:
Stack trace : Caused by: java.lang.NullPointerException at org.apache.tomcat.util.buf.ByteChunk.equalsIgnoreCase(ByteChunk.java:608) at
org.apache.tomcat.util.buf.MessageBytes.equalsIgnoreCase(MessageBytes.java:325)
at org.apache.tomcat.util.http.NamesEnumerator.findNext(MimeHeaders.java:414) at org.apache.tomcat.util.http.NamesEnumerator.nextElement(MimeHeaders.java:438)
at org.apache.tomcat.util.http.NamesEnumerator.nextElement(MimeHeaders.java:396) at generateRequestHeaderMap...
如果你们能帮我解决这个问题就太好了。
3条答案
按热度按时间vql8enpb1#
我怀疑这个问题是由一个损坏的请求引起的。下面是
findNext()
正在做的事情(在Tomcat 6.0.18中):其中的要点如下:
如果头文件被损坏,那么
getName(j)
可能返回null
。如果发生这种情况,那么equalsIgnoreCase
方法的ByteChunk
路径将抛出NPE。如果你要科学地追踪它,你需要:
非科学的方法是将Tomcat升级到Tomcat 6的最新补丁版本......或更高版本。它可能会修复问题。也可能不会。
下面是2010年Tomcat 6.0.20中关于此问题的另一个报告:
9w11ddsr2#
这就是我如何基于Apache Tomcat 6.0.20源代码在JBoss 6.1.0最终版(位于deploy/jbossweb.sar/jbossweb.jar)中成功地修补Apache Tomcat的方法:
文件名枚举器.findNext()
当然,它没有避免https://mail-archives.apache.org/mod_mbox/tomcat-users/201002.mbox/%3c27699460.post@talk.nabble.com%3e中提到的非线程安全实现,但至少避免了在阅读不必要的标头期间出现NullPointerException。
cyvaqqii3#
我知道这很难看,但我就是这么做的: