You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Arnab Ghosh <ma...@yahoo.com> on 2013/02/02 10:31:46 UTC

Overriding Close method of a Output print writer through HttpServletResponseWrapper

Hi,

I need a help for the following problem - 

I have a web application where cookies are getting added multiple times and because of that response header is becoming big containing many duplicate cookies. Now the web application is so big that it is nearly impossible to correct those cookie logic in a short term.

So I have planned to use a HttpServletResponseWrapper in a Filter. It will contain a HashMap. I have overridden the addcookie method of the response wrapper. Now the cookies will be added to the map instead of response. So it will not have any duplicate cookie. Now I have created a Custom Servlet Output Stream and Print writer and overridden the close method. Before closing the stream/writer I will take the cookies from map and will add to the actual response header. Problem is it works fine with pure servlet. But the close method of the stream/writer not getting called when using with JSP. What can be the issue - 

I have overridden the getOutPutStream/Getwriter method and those method will return my custom classes. I have also overridden the flushbuffer method.


Thanks,
Arnab Ghosh    

Re: Overriding Close method of a Output print writer through HttpServletResponseWrapper

Posted by Konstantin Kolinko <kn...@gmail.com>.
2013/2/2 Arnab Ghosh <ma...@yahoo.com>:
>
> I need a help for the following problem -
>
> I have a web application where cookies are getting added multiple times and because of that response header is becoming big containing many duplicate cookies. Now the web application is so big that it is nearly impossible to correct those cookie logic in a short term.
>
> So I have planned to use a HttpServletResponseWrapper in a Filter. It will contain a HashMap. I have overridden the addcookie method of the response wrapper. Now the cookies will be added to the map instead of response. So it will not have any duplicate cookie. Now I have created a Custom Servlet Output Stream and Print writer and overridden the close method. Before closing the stream/writer I will take the cookies from map and will add to the actual response header. Problem is it works fine with pure servlet. But the close method of the stream/writer not getting called when using with JSP. What can be the issue -
>
> I have overridden the getOutPutStream/Getwriter method and those method will return my custom classes. I have also overridden the flushbuffer method.
>


1. Tomcat version = ?

2. response.addCookie() method is a proxy for
response.addHeader("Set-Cookie", ...),
--- See its implementation in org.apache.catalina.connector.Response class.

In a Servlet 3.0 application unless response has been committed, its
headers can be read and overwritten with a combination of
getHeaders(), setHeader(), addHeader() calls.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Overriding Close method of a Output print writer through HttpServletResponseWrapper

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Arnab,

On 2/2/13 4:31 AM, Arnab Ghosh wrote:
> I have a web application where cookies are getting added multiple 
> times and because of that response header is becoming big
> containing many duplicate cookies. Now the web application is so
> big that it is nearly impossible to correct those cookie logic in a
> short term.
> 
> So I have planned to use a HttpServletResponseWrapper in a Filter.
> It will contain a HashMap. I have overridden the addcookie method
> of the response wrapper. Now the cookies will be added to the map
> instead of response. So it will not have any duplicate cookie.

Okay. Presumably, you'll be adding the cookie(s) once at some point.
Obviously, just sticking them into a HashMap won't actually do
anything where the response is concerned. What are you using for the
key in the HashMap?

> Now I have created a Custom Servlet Output Stream and Print writer
> and overridden the close method. Before closing the stream/writer I
> will take the cookies from map and will add to the actual response
> header.

That's an interesting way to do things. I suppose you want only the
latest cookie to be actually added to the response, though, huh?

> Problem is it works fine with pure servlet. But the close method
> of the stream/writer not getting called when using with JSP. What
> can be the issue -

Can you post your code?

> I have overridden the getOutPutStream/Getwriter method and those 
> method will return my custom classes. I have also overridden the 
> flushbuffer method.

Seeing the code would be very helpful.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iEYEAREIAAYFAlEOwf8ACgkQ9CaO5/Lv0PDc5gCeL4JDpZfq8dxMS/wFleQAky8R
YGcAn0lGzzVg0MfvuAcMii40R8eotCdE
=GsD3
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org