在 servlet 文件内调用时如何检查 getwriter 或 getoutputstream 是否已被使用

发布于 2024-12-22 15:03:15 字数 368 浏览 0 评论 0原文

在服务器完成生成 jsp 响应后,我需要在响应中附加一个隐藏标记。我不断收到非法状态异常。所以我使用 getWriter 而不是 getOutputStream 现在它抱怨这一点:

java.lang.IllegalStateException: strict servlet API: cannot call getWriter() after getOutputStream()

在过滤器中传递链,

我在响应包装器中覆盖 getWriter ,我在从该方法返回时

我用 write 附加内容因为我在使用其中之一时遇到错误getWriter 或 getOutputStream,我想我必须以某种方式检查其中哪些已被使用。

I need to write append to the response a hidden tag after the server is done generating the jsp response. I keep getting the illegalstate exception. So I used getWriter instead of getOutputStream now its complaining of this:

java.lang.IllegalStateException: strict servlet API: cannot call getWriter() after getOutputStream()

I am overriding getWriter in a response wrapper which I pass down the chain in my filter

on return from that method I append stuff with write

Since I am getting errors on using either one of getWriter or getOutputStream, I assume I have to check somehow which of these has already been used.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

余生共白头 2024-12-29 15:03:15

由于我在使用 getWriter 或 getOutputStream 之一时遇到错误,我认为我必须以某种方式检查其中哪一个已被使用。

没有标准方法可以做到这一点。

如果您打算使用自定义包装器来实现此功能,则包装器本身需要跟踪是否已调用 getWritergetOutputStream。您可以通过向包装器实现类添加额外的公共方法来公开此状态。

Since I am getting errors on using either one of getWriter or getOutputStream, I assume I have to check somehow which of these has already been used.

There is no standard way to do that.

If you are going to implement this using a custom wrapper, then the wrapper itself needs to keep track of whether getWriter or getOutputStream has been called. You could expose this state by adding an extra public method to your wrapper implementation class.

一世旳自豪 2024-12-29 15:03:15

您可以在使用 servlet 过滤器。第一次实施需要一些工作,但是一旦完成,效果还不错,而且它们非常强大。

You can modify the response headers or data after its been generated with a servlet filter. Its a little bit of work to implement one the first time, but once you've done it its not that bad and they're quite powerful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文