包含带有来自 servlet 的错误重定向的 jsp 页面

发布于 2024-10-10 22:17:59 字数 509 浏览 3 评论 0原文

我试图包含来自 servlet 的 jsp 页面:

  RequestDispatcher rd = ctx.getRequestDispatcher( jspPage );
  rd.include( req, wrapper );   

但出现以下异常:

java.lang.IllegalStateException: Cannot forward after response has been committed

问题在于 JSP 页面通过 JSP 错误标记指定了自己的默认错误页面。 JSP 错误页面也可能引发异常,该异常将逐渐蔓延到 web.xml 中指定的应用程序级别错误页面。因此,当我尝试包含的 jsp 页面抛出异常,并且错误页面也抛出异常时,包含失败。

我必须优雅地处理这种情况,因为我在页面上包含了用户编写的模块,并且错误的模块应该向用户显示异常,而不是使用 IllegalStateException 进行轰炸。有什么想法吗?

I'm trying to include a jsp page from a servlet:

  RequestDispatcher rd = ctx.getRequestDispatcher( jspPage );
  rd.include( req, wrapper );   

But I'm getting the following exception:

java.lang.IllegalStateException: Cannot forward after response has been committed

The problem is with a JSP page that specifies its own default error page through the JSP error tag. The JSP error page can also throw an exception which will trickle up to the application level error page specified in web.xml. So when the jsp page that I am trying to include throws an exception, and the error page throws an exception as well, the include fails.

I have to handle this case gracefully because I am including user-written modules on a page and an erroneous module should display the exception to the user rather than bomb with the IllegalStateException. Any ideas?

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

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

发布评论

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

评论(2

冰雪之触 2024-10-17 22:17:59

正如您的上一个问题中的评论中已经暗示的那样,它只是<更改响应为时已晚。在响应中的 X 个字节之后,响应标头将被提交给客户端,并且这是不返回的点。您需要重写 JSP。它们应该只包含标记和表示逻辑,而不包含任何可能引发异常的业务逻辑。

如果您确实无法将 JSP 中的错误业务代码重构为完整的 servlet/业务类(它们所属的位置),那么您最好的选择是增加服务器配置中的 HTTP 响应缓冲区大小,并祈祷没有 在 JSP 中发生异常之前调用了lush(),并且当 Web 应用程序繁忙时,您不会遇到 OutOfMemoryError 失败。

相关问题:

As already hinted in a comment in your previous question, it's simply too late to change the response. After a X amount of bytes in the response, the response headers will be committed to the client and this is a point of no return. You need to rewrite your JSP's. They should contain only markup and presentation logic and not any business logic which can throw exceptions.

If you really can't refactor the erroneous business code in your JSP's into fullworthy servlet/business classes, there where they belongs, then your best bet is to increase the HTTP response buffer size in the server config and pray that nowhere a flush() is been invoked before the exception occurs in the JSP and you don't get OutOfMemoryError failures when it gets busy on your webapp.

Related questions:

泪之魂 2024-10-17 22:17:59

您可以尝试将用户的浏览器重定向到错误页面吗?您可能需要在会话中保存要向用户显示的任何错误信息,以便错误页面可以显示它。

唯一的问题是写入重定向消息可能会失败,因为内容已经从错误的“页面”传递。不确定它是否非常防弹:)

可能增加缓冲区大小可能会使其在更多页面上工作。

You could try redirecting the user's browser to the error page? You might need to save any error information you want to display to the user in the session, so the error page can display it.

The only issue is writing the redirect message might fail as content has already been delivered from the faulty 'page'. Not sure it's very bulletproof :)

Possibly increasing the buffer size might make it work on more pages.

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