如何从标记库内部停止进一步的请求执行?

发布于 2024-08-14 08:35:05 字数 115 浏览 4 评论 0原文

基本上,我们想要创建一个可以将请求执行转发到另一个页面的标记库,类似于 。我们如何防止 JSP 页面的其余部分从 taglib 内执行?

Basically, we want to create a taglib that can possibly forward request execution to another page, similar to <jsp:forward />. How can we prevent the rest of the JSP page from executing from within the taglib?

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

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

发布评论

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

评论(3

坠似风落 2024-08-21 08:35:05

您不应该在标记库中执行此操作。而是在 Servlet过滤,在将任何位发送到响应之前。否则,您可能会遇到 IllegalStateException:response already commit 麻烦。

You shouldn't be doing this in a taglib. Rather do it in a Servlet or Filter, before any bit is been sent to the response. Otherwise you will possible get into IllegalStateException: response already committed troubles.

差↓一点笑了 2024-08-21 08:35:05

对我来说,以下几行效果很好:

String url = "http://google.com";

response.reset();
response.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
response.setHeader("Location", url);
response.getWriter().close();
response.getWriter().flush();

For me the following lines worked quite well:

String url = "http://google.com";

response.reset();
response.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
response.setHeader("Location", url);
response.getWriter().close();
response.getWriter().flush();
夏至、离别 2024-08-21 08:35:05

如果您不想在将控制权传递给另一个页面后继续处理原始页面,那么最好使用重定向而不是转发。

response.sendRedirect("foo.jsp");

这将重定向到新页面并停止处理旧页面。

但是 - 仅当您尚未向响应正文写入任何内容(即未将任何数据发送回客户端)时,重定向才可用。

If you don't want to continue processing the original page after passing control to another you are better off using a redirect rather than a forward.

response.sendRedirect("foo.jsp");

This will redirect to the new page and stop processing the old one.

However - redirects are only usable if you have not written anything to the response body yet (i.e. not sent any data back to the client).

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