1

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.

Cemo
  • 5,370
  • 10
  • 50
  • 82
  • Hi, don't really get what you are trying to achieve, the question [In spring mvc 3, how to write a cookie while returning a ModelAndView?](http://stackoverflow.com/questions/4888456/in-spring-mvc-3-how-to-write-a-cookie-while-returning-a-modelandview) might be interesting. Maybe you could add some code to your question. – Wolfram Sep 29 '11 at 14:52
  • I have updated little. If it is not still clear, I will explain detailed. – Cemo Sep 29 '11 at 14:57

1 Answers1

1

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I am aware of this but this is totally different. I dont have any exception. For example I can still write with 'response.getWriter().print()' at my jsp. I mean it is not commited. – Cemo Sep 30 '11 at 07:25
  • That can happen if it's been JSP-included by ``. That line has to go in the main JSP. – BalusC Sep 30 '11 at 13:09
  • Yes I do include :) I will check to inform you. But can you point me a documentation regarding it? Because the behaviour is changing from Jetty to Glassfish also too. – Cemo Oct 01 '11 at 15:02
  • `` uses `RequestDispatcher#include()` under the covers. Read its Javadoc with regard to modifying the response headers: http://download.oracle.com/javaee/6/api/javax/servlet/RequestDispatcher.html#include%28javax.servlet.ServletRequest,%20javax.servlet.ServletResponse%29 – BalusC Oct 01 '11 at 15:34
  • BalusC can you modify your answer regarding include. It can better to be understood. I will also modify my question. Thank you very much. – Cemo Oct 01 '11 at 16:17