使用 Spring MVC 更改包含的 JSP 中的标头
这对我来说非常有趣,但我有简单的 Spring MVC 应用程序和 JSP 页面。在包含的 Jsp 页面中,我想向我的应用程序添加一个 cookie。然而,尽管设置了它,但它无法在运行时解决。
这是我的 include jsp 页面上的代码。
<% response.addCookie(new Cookie("test3","test3")); %>
我更喜欢在 jsp 级别编写应用程序的某些部分,而不是在控制器上编写。
我只能说,当我调用我的方法时,我正在使用 Tuckey UrlRewrite 和 at 而不是我的 jsp 页面,它工作正常。在我调用的方法中,我可以看到 MVC 控制器中的初始响应对象被另一个 HttpServletResponse 对象包装。好像转发到jsp后headers和cookie就不能改了?
有什么帮助吗?
PS:我已经更新了我的问题,以明确它是包含jsp的页面。
It is very interesting for me but I have simple Spring MVC application and JSP page. At Jsp pages which are included I would like to add a cookie to my application. However despite of setting it, It could not resolved at runtime.
this is the code at my included jsp page.
<% response.addCookie(new Cookie("test3","test3")); %>
I prefer writing some of the parts of the our application at jsp level over writing at controller.
What I can just say is that I am using Tuckey UrlRewrite and at instead of my jsp pages when I call my method, it is working fine. And at my called method I can see that the inital response object at my MVC controller is wrapped by another HttpServletResponse object. It seems that headers and cookies could not be changed after forwarded to jsp?
Any help?
PS: I have updated my question to make it clear regarding it is jsp included page.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JSP 是响应的一部分。您需要确保在提交响应之前执行该行。否则,您最终会在服务器日志中看到
IllegalStateException:响应已提交
。因此,在将任何 HTML 发送到响应之前,将其放在 JSP 页面的最顶部。或者,更好的方法是,在转发到 JSP 之前将其放入 Spring 控制器、servlet 或过滤器中。您还需要确保没有更改
包含的 JSP 文件内的响应。它将被简单地忽略。另请参阅RequestDispatcher#include()
javadoc:JSP is part of the response. You need to ensure that that line is exeucted before the response is been committed. Otherwise you end up with
IllegalStateException: response already committed
in the server logs. So put it in the very top of the JSP page, before any HTML is been sent to the response. Or, better, just put it in a Spring controller or a servlet or filter, far before the forward to JSP takes place.You also need to ensure that you aren't altering the response inside a JSP file which is included by
<jsp:include>
. It will be plain ignored. See also theRequestDispatcher#include()
javadoc: