简体   繁体   English

来自 jsp 的 response.sendRedirect():包括被忽略?

[英]response.sendRedirect() from jsp:include being ignored?

I've got a jsp file, which includes another jsp file to check some values and such:我有一个 jsp 文件,其中包括另一个 jsp 文件来检查一些值等:

<jsp:include page="setup.jsp" />

Inside the setup.jsp I've got some conditional code which determines if some needed values are set in the session and if not redirects them to a different page.在 setup.jsp 中,我有一些条件代码,用于确定是否在 session 中设置了一些需要的值,如果没有,则将它们重定向到不同的页面。 Or at least it is supposed to, but the redirect seems to be getting ignored.或者至少应该如此,但重定向似乎被忽略了。

System.err.println("Redirecting!");
response.sendRedirect("http://www.google.com");
return;

I see "Redirecting," get logged to the console.我看到“重定向”,登录到控制台。 but the page continues on and renders normally.但页面继续并正常呈现。 I had curl dump the headers for me and saw that the response is HTTP/1.1 200 OK so it definitely isn't sending a 302 redirect.我让 curl 为我转储了标头,并看到响应是HTTP/1.1 200 OK ,所以它肯定没有发送 302 重定向。

Any idea what the problem is and how I can fix this?知道问题是什么以及如何解决这个问题吗?

EDIT: I have verified that my response is not yet committed.编辑:我已经确认我的回复尚未提交。 response.isCommitted() returns false meaning the status code and headers have not been sent yet. response.isCommitted()返回false表示状态代码和标头尚未发送。

EDIT 2: I've tried calling response.sendRedirect() in many other places and find that I can successfully redirect before the.编辑 2:我尝试在许多其他地方调用response.sendRedirect()并发现我可以在之前成功重定向。 The redirect inside the JSP seems to be ignored and if I try to redirect right AFTER the jsp then I get an illegal state exception because the response has already been committed. JSP 内的重定向似乎被忽略了,如果我尝试在 jsp 之后正确重定向,那么我会收到一个非法的 state 异常,因为响应已经提交。

The <jsp:include> uses under the covers RequestDispatcher#include() . <jsp:include>在幕后使用RequestDispatcher#include() Click the link to see the javadoc.单击链接以查看 javadoc。 Here's an extract of relevance (emphasis mine):这是相关性的摘录(强调我的):

... ...

The ServletResponse object has its path elements and parameters remain unchanged from the caller's. ServletResponse object 的路径元素和参数与调用者的保持不变。 The included servlet cannot change the response status code or set headers;包含的 servlet 不能更改响应状态码或设置标头; any attempt to make a change is ignored.任何进行更改的尝试都会被忽略。

... ...

The HttpServletResponse#sendRedirect() basically sets the HTTP response status to 302 and the HTTP Location header to the target URL. The HttpServletResponse#sendRedirect() basically sets the HTTP response status to 302 and the HTTP Location header to the target URL. It is thus totally being ignored.因此,它完全被忽略了。

The deeper problem is that you're abusing JSP as a page controller/filter.更深层次的问题是您滥用 JSP 作为页面控制器/过滤器。 You should actually be using a normal servlet or filter class for this which runs far before JSP.您实际上应该为此使用普通的servlet过滤器class,它在 JSP 之前运行。

The redirect header (I believe) needs to be at the top of the page.重定向 header (我相信)需要位于页面顶部。 View the HTML spec for reference.查看 HTML 规范以供参考。

Have you tried placing it at the very top of the page?您是否尝试将其放在页面的最顶部? A code sample would help us debug...代码示例将帮助我们调试...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM