This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Transfer Learning for Natural Language Processing (MEAP) and have Paul Azunre on-line!
See this thread for details.
Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Add Cookie to HttpServletResponseWrapper

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I need to set a cookie on the response in one of my servlets. That servlet is being called from a requestDispatcher.include method so I'm not able to change the request headers directly in my servlet. I was hoping to use an HttpServletResponseWrapper in a Filter. Something like:

HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper ((HttpServletResponse)response);

chain.doFilter(request, wrapper);

Cookie cookie = new Cookie( "TEST2", "TESTING");
cookie.setPath("/");
cookie.setMaxAge(325);
wrapper.addCookie(cookie);

Obviously I'm doing something wrong because the cookie is not getting set. Is this even possible? Please advise.
Thank you in advance!!
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the include is called, how much has already been written to the response?

If the initial buffer load has already been sent, then so have all the response headers, and you will not be able to do anything to affect them.

Bill
 
Che Frenz
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bill,

Bare with me. I'm not 100% clear on exactly what gets written to the response when.

My first servlet does some work (logs out of weblogic) then sends include to my next servlet which does some work (logs out of adrenaline) then my first servlet sends a redirect to a jsp.

Neither servlet specifically writes to the response or manipulates the response except for my attempt by the filter.

Thanks for your help!
 
Che Frenz
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again,

I should revise my post. At the end of the day my real problem is:

I have a requirement to call a servlet (servlet2) from another servlet (servlet1) where servlet2 logs out of a system and kills the associated session cookies. I do not want to forward to servlet2 because servlet1 needs to control the redirect to the appropriate jsp.

In this type of situation what would you recommend as the best approach? Is it even doable?

Thanks!
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are the functions in servlet2 even in a servlet?

Sounds very confusing to me, maybe you should back off and redesign.

Bill
 
Che Frenz
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bill,

That sounds like a really good idea. At least I won't spin my wheels trying to do something that really won't work.

Thanks for you time!
 
I've got no option but to sell you all for scientific experiments. Or a tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic