Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Adding a cookie to a response in servlet filter

23 views
Skip to first unread message

Arundhat...@csx.com

unread,
Feb 20, 2008, 7:43:08 PM2/20/08
to
Hello,<br />
<br />
I need to add a cookie to the response in a servlet filter after calling the doFilter method. I know I can add it prior to the call and that works but my requirement is to add it after the call. <br />
<br />
I tried different approaches like response wrapper and extending output stream to no avail. I always get an error like below<br />
<br />
2/10/08 9:33:35:487 EST 00000041 SRTServletRes W WARNING: Cannot set header. Response already committed.<br />
<br />
Any help anyone can provide me with this, possibly with code samples / examples, will be greatly appreciated.<br />
<br />
Thank you

Arundhat...@csx.com

unread,
Feb 20, 2008, 7:44:09 PM2/20/08
to

Ben_

unread,
Feb 21, 2008, 10:18:09 AM2/21/08
to
A cookie is a HTTP response header.

Once you start writing the HTTP response body, you cannot add an HTTP
response header.

This is easily understood when you realize the HTTP is streamed to the
client. Simply put, when the data is sent, it's too late to recall it.

So, you need to re-organize your logic to add the Cookie before writing the
body.

Arundhat...@csx.com

unread,
Feb 23, 2008, 7:07:28 PM2/23/08
to
I am not fully conversant with this and not completely sure about it, but I had thought that using the responsewrapper and extending the outputstream and using that with the wrapper overcomes this. ( Tried it and didn't work but I thought that I was not doing it right ) <br />
<br />
Is that not true? <br />
<br />
Thanks.

Ben_

unread,
Feb 24, 2008, 12:51:48 AM2/24/08
to
I don't know if wrapping and extending will help you avoid the problem.

In all cases, at the end, the order will have to be correct, as you cannot
add another HTTP header after you started writing the HTTP body.

Arundhat...@csx.com

unread,
Feb 24, 2008, 10:46:55 AM2/24/08
to
Thanks. That helps me understand the cause of the problem.

Arundhat...@csx.com

unread,
Feb 25, 2008, 8:23:26 AM2/25/08
to
I was looking at examples on sun site, http://courses.coreservlets.com/Course-Materials/pdf/msajsp/05-Filters.pdf and some others where the filter changes the encoding or content type or content length AFTER chain.doFilter method call, using the wrapper and outputstream. So, the headers are being changed after the doFilter call. I am just not able to get things to work for cookies. That is why I thought, I am missing something, but that it should be possible. <br />
<br />
Any thoughts?

Paul Ilechko

unread,
Feb 25, 2008, 9:18:53 AM2/25/08
to

It depends on what you are doing in the filter ...

Arundhat...@csx.com

unread,
Feb 25, 2008, 10:22:14 AM2/25/08
to
Can you explain that a little more? Or are there any documents / link / books that would explain in details what can a nd can not be done? <br />
<br />
My confusion here is why some things in the header can be changed but not others.<br />
<br />
Thanks.
0 new messages