HttpServletRequest#getRemoteAddr() 返回 NULL
为什么 HttpServletRequest.getRemoteAddr()
返回null
有时?对于大约十分之二的相同请求,会无缘无故地返回 null
。
Why does HttpServletRequest.getRemoteAddr()
return null
sometimes? For approximately 2 out of 10 identical requests is returns null
for no apparent reason.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用Tomcat7。显然,如果请求已被使用,即已发送响应,则
getRemoteAddr()
可以返回NULL
。有时它甚至会抛出 NPE。无论如何,我的代码中有一个错误。Using Tomcat7. Apparently
getRemoteAddr()
can returnNULL
if the request has already been consumed, i.e. a response has been sent. It can even throw NPE sometimes. An error in my code in any case.您是否将请求对象传递给另一个线程以异步读取它?在某些 Web 容器中,一旦 servlet 线程完成,请求对象将被回收/重置。那么当调用
request.getRemoteAddr()
时,您可能会得到null
。Did you pass the request object to another thread to read it asynchronously? In some web container, request objects will be recycled/reset once the servlet thread is finished. Then you might get
null
whenrequest.getRemoteAddr()
is called.这可能是所使用的特定 servlet 容器或代理中的错误或配置错误。查找您正在使用的版本,检查其版本并检查此错误是否已报告给其维护者,并检查是否有可以升级到的新版本。
That would be either a bug or a misconfiguration in the specific servletcontainer or proxy used. Lookup which one you are using, check its version and check if this bug has been reported before to its maintainers and check if there is a newer version to which you can upgrade.