Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Add Cookie to HttpServletResponseWrapper

 
Che Frenz
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!!
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
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
Rancher
Posts: 13074
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic