• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

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]
 
If you want to look young and thin, hang around old, fat people. Or this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic