调用request.getReader()后重置HttpRequest [英] resetting a HttpRequest after calling request.getReader()

查看:725
本文介绍了调用request.getReader()后重置HttpRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 HttpRequest 上调用 getReader()方法,然后重置请求所以对 getReader()的其他调用不会抛出 java.lang.IllegalStateException

Is there a way to call the getReader() method on a HttpRequest and then "reset" the request so other calls to getReader() will not throw java.lang.IllegalStateException?

推荐答案

简单的答案是不。

该流不可重置,并且没有API方法可以让您重新打开它。 (并且有充分的理由。它需要servlet基础结构保留输入的副本,以防万一 servlet决定重新打开流。这将是一个无根据的开销。)

The stream is not resettable and there's no API method that will allow you to reopen it. (And for good reason. It would require the servlet infrastructure to keep a copy of the input just in case the servlet decided to reopen the stream. That would be an unwarranted overhead.)

如果你想做这种事情,你需要编写代码来保存自己的数据副本。如果你在Filter(或Tomcat Valve)中实现它,那么你可以创建一个 HttpServletRequestWrapper 来隐藏你已经读过数据的事实....由@Vineet建议。

If you want to do this kind of thing, you will need to write your code to keep its own copy of the data. If you are implementing this in a Filter (or a Tomcat Valve) then you could create a HttpServletRequestWrapper to hide the fact that you've already read the data .... as suggested by @Vineet.

这篇关于调用request.getReader()后重置HttpRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆