• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 17424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]
 
reply
    Bookmark Topic Watch Topic
  • New Topic