aspose file tools*
The moose likes Servlets and the fly likes Add Cookie to HttpServletResponseWrapper Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Add Cookie to HttpServletResponseWrapper" Watch "Add Cookie to HttpServletResponseWrapper" New topic
Author

Add Cookie to HttpServletResponseWrapper

Che Frenz
Greenhorn

Joined: Dec 15, 2008
Posts: 14
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

Joined: Mar 22, 2000
Posts: 12806
    
    5
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

Joined: Dec 15, 2008
Posts: 14
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

Joined: Dec 15, 2008
Posts: 14
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

Joined: Mar 22, 2000
Posts: 12806
    
    5
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

Joined: Dec 15, 2008
Posts: 14
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Add Cookie to HttpServletResponseWrapper