开发者

Change header at Included JSP with Spring MVC

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开发者_JS百科 question to make it clear regarding it is jsp included page.


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 the 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜