wood burning stoves 2.0*
The moose likes JForum and the fly likes Why the Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Products » JForum
Bookmark "Why the "response.addCookie(..)" can Watch "Why the "response.addCookie(..)" can New topic
Author

Why the "response.addCookie(..)" can't work in the filter?

Migrated From Jforum.net
Ranch Hand

Joined: Apr 22, 2012
Posts: 17424
Hello,everybody!

The problem is that:

I want to add/refresh a cookie to response after processed ervery page, so
I put a servlet filter in the web.xml, the web.xml looks like that:


and the SSOFilter.java is below:



But there is no cookie in the Browser after refresh the page. Howerver, these codes work well in other web applications.
why is it? and what should I do to add a cookie after the *.page processing.

Thanks.
[originally posted on jforum.net by aaron]
Migrated From Jforum.net
Ranch Hand

Joined: Apr 22, 2012
Posts: 17424
Adding a cookie requires modifying the HTTP headers of the response. Once the response has been "committed", e.g. anything written to the output stream, you can not modify the HTTP headers. The response has been committed by the time you try to add your cookie.
[originally posted on jforum.net by monroe]
Migrated From Jforum.net
Ranch Hand

Joined: Apr 22, 2012
Posts: 17424
thans for the reply.
yes, I put the "addCookie" before "chain.filter(...)"
and it works.
and the only problem is:
why it can works in some web application?
[originally posted on jforum.net by aaron]
Migrated From Jforum.net
Ranch Hand

Joined: Apr 22, 2012
Posts: 17424
Most JSP implimentations and some servlets will actually generate the response in a buffer. This means the response isn't committed the output is closed or manually flushed. So, this would work in some applications and not in others.

I imagine that the either jForum or the Template engine does a specific flush of the output stream at some point.

FWIW, there is also a JSP page directive to set the buffer size and turn on autoflush. These are useful for long running pages that you want users to see some status info coming back. Your cookie setting method would fail for pages like these too.

[originally posted on jforum.net by monroe]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why the "response.addCookie(..)" can't work in the filter?
 
Similar Threads
Facing Issue in implementing Listeners and Filters
No grammar found web.xml
NullPointerException in AutoScrollPhaseListener
Problem with session timeout
richfaces and problems with navigation