java 来自 jsp:include 的 response.sendRedirect() 被忽略?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6637990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:44:41  来源:igfitidea点击:

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

javajspredirectjspinclude

提问by Kenny Wyland

I've got a jsp file, which includes another jsp file to check some values and such:

我有一个 jsp 文件,其中包含另一个用于检查某些值的 jsp 文件,例如:

<jsp:include page="setup.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. Or at least it is supposed to, but the redirect seems to be getting ignored.

在 setup.jsp 中,我有一些条件代码,用于确定是否在会话中设置了一些所需的值,如果没有,则将它们重定向到不同的页面。或者至少应该这样做,但重定向似乎被忽略了。

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 OKso 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 falsemeaning 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 . 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.

编辑 2:我尝试response.sendRedirect()在许多其他地方打电话,发现我可以在 . JSP 内部的重定向似乎被忽略,如果我尝试在 jsp 之后立即重定向,那么我会收到非法状态异常,因为响应已经提交。

回答by BalusC

The <jsp:include>uses under the covers RequestDispatcher#include(). Click the link to see the javadoc. Here's an extract of relevance (emphasis mine):

幕后<jsp:include>用途RequestDispatcher#include()。单击链接以查看 javadoc。这是相关性的摘录(强调我的):

...

The ServletResponse object has its path elements and parameters remain unchanged from the caller's. The included servlet cannot change the response status code or set headers; any attempt to make a change is ignored.

...

...

ServletResponse 对象的路径元素和参数与调用者的路径元素和参数保持不变。包含的 servlet 不能更改响应状态代码或设置标头;任何进行更改的尝试都将被忽略。

...

The HttpServletResponse#sendRedirect()basically sets the HTTP response status to 302and the HTTP Locationheader to the target URL. It is thus totally being ignored.

HttpServletResponse#sendRedirect()基本上设置HTTP响应状态302和HTTPLocation报头到目标URL。因此,它完全被忽略了。

The deeper problem is that you're abusing JSP as a page controller/filter. You should actually be using a normal servletor filterclass for this which runs far before JSP.

更深层次的问题是您滥用 JSP 作为页面控制器/过滤器。您实际上应该为此使用一个正常的servlet过滤器类,它在 JSP 之前运行很远。

回答by WattsInABox

The redirect header (I believe) needs to be at the top of the page. 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...

你有没有试过把它放在页面的最顶部?代码示例将帮助我们调试...