You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by banto <ba...@gmail.com> on 2010/05/21 23:47:39 UTC

set-cookie

Hi gurus,

my problem here is that i'm trying to understnad a servlet code that makes
tomcat to send the

Set-Cookie: JSESSIONID=8BCB60D6F6B9394B4ABD2FDD007BDB39; Path=/store

header.
Now the code is something like:

public class FrontController extends HttpServlet {

	public void init() throws ServletException {

		HashMap products = new HashMap();
		Product p = new Product(1, "Dog", "9.99");
		products.put("1", p);
		//  Store products in the ServletContext
		getServletContext().setAttribute("products", products);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse
response)
			.....

and the web.xml is quite standard. 
my problem is that i don´t see where the servlet is instructing tomcat to
sent that header.

My other servlets are not issuing that header when running. So what i´m
missing?

thanks you!!
-- 
View this message in context: http://old.nabble.com/set-cookie-tp28639362p28639362.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


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


RE: set-cookie

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: banto [mailto:banto.78@gmail.com]
> Subject: set-cookie
> 
> Now the code is something like:
> 
> public class FrontController extends HttpServlet {
> 
> 	public void init() throws ServletException {
> 
> 		HashMap products = new HashMap();
> 		Product p = new Product(1, "Dog", "9.99");
> 		products.put("1", p);
> 		//  Store products in the ServletContext
> 		getServletContext().setAttribute("products", products);
> 	}
> 	public void doPost(HttpServletRequest request,
> HttpServletResponse
> response)
> 			.....

It's the code in the doXxx() methods that needs examination, not the init() method.  Do you have any JSPs?  IIRC, these will automatically create a session unless explicitly disabled.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.


RE: set-cookie

Posted by Martin Gainty <mg...@hotmail.com>.
//if we reached here, no exception, cookie is valid the header name is Set-Cookie for both "old" and v.1 ( RFC2109 )
//RFC2965 is not supported by browsers and the Servlet spec asks for 2109.

//in any case the cookie is added to the response header with Set-Cookie keyword as seen here
Response.addHeader("Set-Cookie", string);

?
Martin--
______________________________________________ 
Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité

 
Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.
Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le destinataire prévu, nous te demandons avec bonté que pour satisfaire informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est interdite. Ce message sert à l'information seulement et n'aura pas n'importe quel effet légalement obligatoire. Étant donné que les email peuvent facilement être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité pour le contenu fourni.



 

> Date: Sat, 22 May 2010 01:15:15 -0700
> From: banto.78@gmail.com
> To: users@tomcat.apache.org
> Subject: RE: set-cookie
> 
> 
> ..then where the 
> 
> set-cookie come from if not from the line
> getServletContext().setAttribute("products", products);?
> 
> actually i added this line to another servlet but no
> set-cookie header is sent?
> 
> thanks
> 
> n828cl wrote:
> > 
> >> From: André Warnier [mailto:aw@ice-sa.com]
> >> Subject: Re: set-cookie
> >> 
> >> // Store products in the ServletContext
> >> getServletContext().setAttribute("products", products);
> >> 
> >> Presumably, if you store something in the context, it is because you
> >> want to retrieve it later (or else, what's the point he ?).
> >> Now, Tomcat has no way to know when you'll come back to retrieve it, so
> >> it has to create a session to store this.
> > 
> > Nope, that's not it (but I don't know what is, yet). Attributes stored in
> > a ServletContext have a webapp, not session, scope; no session is needed
> > here, and nothing needs to be communicated between the client and server
> > to retrieve them.
> > 
> > - Chuck
> > 
> > 
> > THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
> > MATERIAL and is thus for use only by the intended recipient. If you
> > received this in error, please contact the sender and delete the e-mail
> > and its attachments from all computers.
> > 
> > 
> > 
> 
> -- 
> View this message in context: http://old.nabble.com/set-cookie-tp28639362p28641797.html
> Sent from the Tomcat - User mailing list archive at Nabble.com.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 
 		 	   		  
_________________________________________________________________
Hotmail is redefining busy with tools for the New Busy. Get more from your inbox.
http://www.windowslive.com/campaign/thenewbusy?ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_2

RE: set-cookie

Posted by banto <ba...@gmail.com>.
great guys for a great forum!!!



n828cl wrote:
> 
>> From: banto [mailto:banto.78@gmail.com]
>> Subject: Re: set-cookie
>> 
>> i´m using something like
>> System.out.println(Thread.currentThread().getStackTrace());
> 
> What you got was a display of the array object - not very interesting. 
> Either iterate through the array, displaying each entry, or just do this:
> 
>     Thread.currentThread().dumpStack();
> 
>  - Chuck
> 
> 
> THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
> MATERIAL and is thus for use only by the intended recipient. If you
> received this in error, please contact the sender and delete the e-mail
> and its attachments from all computers.
> 
> 
> 

-- 
View this message in context: http://old.nabble.com/set-cookie-tp28639362p28664798.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


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


Re: set-cookie

Posted by Kris Schneider <ks...@gmail.com>.
On Wed, May 26, 2010 at 2:04 PM, Christopher Schultz
<ch...@christopherschultz.net> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Chuck,
>
> On 5/24/2010 3:55 PM, Caldarale, Charles R wrote:
>>> From: banto [mailto:banto.78@gmail.com]
>>> Subject: Re: set-cookie
>>>
>>> i´m using something like
>>> System.out.println(Thread.currentThread().getStackTrace());
>>
>> What you got was a display of the array object - not very interesting.  Either iterate through the array, displaying each entry, or just do this:
>>
>>     Thread.currentThread().dumpStack();
>
> Or the more traditional:
>
> new Throwable("Stack Dump").printStackTrace();

dumpStack is actually static, so:

Thread.dumpStack();

which is implemented as:

new Exception("Stack trace").printStackTrace();

> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.10 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAkv9YrcACgkQ9CaO5/Lv0PDXegCfeKZOUdIKnIgAnfSmrhO015D2
> lVEAnRzbT4mKvoli8WDYshlA1uNGHHQS
> =jp7W
> -----END PGP SIGNATURE-----

-- 
Kris Schneider

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


RE: set-cookie

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Christopher Schultz [mailto:chris@christopherschultz.net]
> Subject: Re: set-cookie
> 
> >     Thread.currentThread().dumpStack();
> 
> Or the more traditional:
> 
> new Throwable("Stack Dump").printStackTrace();

And if we look inside java.lang.Thread, we find:

    public static void dumpStack() {
	new Exception("Stack trace").printStackTrace();
    }

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.


Re: set-cookie

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

Chuck,

On 5/24/2010 3:55 PM, Caldarale, Charles R wrote:
>> From: banto [mailto:banto.78@gmail.com]
>> Subject: Re: set-cookie
>>
>> i´m using something like
>> System.out.println(Thread.currentThread().getStackTrace());
> 
> What you got was a display of the array object - not very interesting.  Either iterate through the array, displaying each entry, or just do this:
> 
>     Thread.currentThread().dumpStack();

Or the more traditional:

new Throwable("Stack Dump").printStackTrace();

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkv9YrcACgkQ9CaO5/Lv0PDXegCfeKZOUdIKnIgAnfSmrhO015D2
lVEAnRzbT4mKvoli8WDYshlA1uNGHHQS
=jp7W
-----END PGP SIGNATURE-----

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


RE: set-cookie

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: banto [mailto:banto.78@gmail.com]
> Subject: Re: set-cookie
> 
> i´m using something like
> System.out.println(Thread.currentThread().getStackTrace());

What you got was a display of the array object - not very interesting.  Either iterate through the array, displaying each entry, or just do this:

    Thread.currentThread().dumpStack();

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.


Re: set-cookie

Posted by banto <ba...@gmail.com>.
chris,

useful info. however when i print the current thread stack trace with a
HttpSessionListener
i see few info; something like this

Ljava.lang.STackTraceElement;@4313455

i´m using something like
System.out.println(Thread.currentThread().getStackTrace());

in the sessionCreated().

any advice?

thanks

Christopher Schultz-2 wrote:
> 
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Banto,
> 
> On 5/22/2010 12:01 PM, banto wrote:
>> 
>> Hi Chuck,
>> 
>> i have found out who is generating that in my code:
>> 
>> dispatcher.forward(request, response);
>> 
>> the response contains also the set cookie header...it is automatically
>> done
>> by tomcat...
> 
> You can "safely" call RequestDispatcher.forward() without sending a
> Set-Cookie header. The code to your doPost method might be helpful,
> here. If you do any of the following, Tomcat will create a session for
> you and send a Set-Cookie header for the session:
> 
> 1. directly call request.getSession() or request.getSession(true)
> 
> 2. forward-to or include a JSP that does not have session="false"
>    declared in the <%@ page %> directive
> 
> If you want to know what code it creating the session, write an
> HttpSessionListener, configure it in web.xml, and have your
> sessionCreated() method print the stack trace of the current thread.
> 
> Hope that helps,
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.10 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
> 
> iEYEARECAAYFAkv6jkAACgkQ9CaO5/Lv0PCvngCggPq3JYga/w4O0yy8/1Vnua5t
> /QEAnjS1/bSZ87u1ie6aO2omXuWkzCim
> =Bf+a
> -----END PGP SIGNATURE-----
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 
> 
> 

-- 
View this message in context: http://old.nabble.com/set-cookie-tp28639362p28661057.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


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


Re: set-cookie

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

Banto,

On 5/22/2010 12:01 PM, banto wrote:
> 
> Hi Chuck,
> 
> i have found out who is generating that in my code:
> 
> dispatcher.forward(request, response);
> 
> the response contains also the set cookie header...it is automatically done
> by tomcat...

You can "safely" call RequestDispatcher.forward() without sending a
Set-Cookie header. The code to your doPost method might be helpful,
here. If you do any of the following, Tomcat will create a session for
you and send a Set-Cookie header for the session:

1. directly call request.getSession() or request.getSession(true)

2. forward-to or include a JSP that does not have session="false"
   declared in the <%@ page %> directive

If you want to know what code it creating the session, write an
HttpSessionListener, configure it in web.xml, and have your
sessionCreated() method print the stack trace of the current thread.

Hope that helps,
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkv6jkAACgkQ9CaO5/Lv0PCvngCggPq3JYga/w4O0yy8/1Vnua5t
/QEAnjS1/bSZ87u1ie6aO2omXuWkzCim
=Bf+a
-----END PGP SIGNATURE-----

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


RE: set-cookie

Posted by banto <ba...@gmail.com>.
Hi Chuck,

i have found out who is generating that in my code:

dispatcher.forward(request, response);

the response contains also the set cookie header...it is automatically done
by tomcat...



n828cl wrote:
> 
>> From: banto [mailto:banto.78@gmail.com]
>> Subject: RE: set-cookie
>> 
>> ..then where the
>> 
>> set-cookie come from if not from the line
>> getServletContext().setAttribute("products", products);?
> 
> That's part of the init() method, called when the servlet initializes, not
> when it's processing a request.  There's no request or response involved
> at that time, so it's impossible for that code to have any relationship
> with cookie generation or evaluation (or session creation).
> 
> See my previous response - you're creating a session somewhere, so Tomcat
> generates the appropriate cookie when that happens.
> 
>  - Chuck
> 
> 
> THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
> MATERIAL and is thus for use only by the intended recipient. If you
> received this in error, please contact the sender and delete the e-mail
> and its attachments from all computers.
> 
> 
> 

-- 
View this message in context: http://old.nabble.com/set-cookie-tp28639362p28644188.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


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


RE: set-cookie

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: banto [mailto:banto.78@gmail.com]
> Subject: RE: set-cookie
> 
> ..then where the
> 
> set-cookie come from if not from the line
> getServletContext().setAttribute("products", products);?

That's part of the init() method, called when the servlet initializes, not when it's processing a request.  There's no request or response involved at that time, so it's impossible for that code to have any relationship with cookie generation or evaluation (or session creation).

See my previous response - you're creating a session somewhere, so Tomcat generates the appropriate cookie when that happens.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.


RE: set-cookie

Posted by banto <ba...@gmail.com>.
..then where the 

set-cookie come from if not from the line
getServletContext().setAttribute("products", products);?

actually i added this line to another servlet but no
set-cookie header is sent?

thanks

n828cl wrote:
> 
>> From: André Warnier [mailto:aw@ice-sa.com]
>> Subject: Re: set-cookie
>> 
>>     //  Store products in the ServletContext
>> 	getServletContext().setAttribute("products", products);
>> 
>> Presumably, if you store something in the context, it is because you
>> want to retrieve it later (or else, what's the point he ?).
>> Now, Tomcat has no way to know when you'll come back to retrieve it, so
>> it has to create a session to store this.
> 
> Nope, that's not it (but I don't know what is, yet).  Attributes stored in
> a ServletContext have a webapp, not session, scope; no session is needed
> here, and nothing needs to be communicated between the client and server
> to retrieve them.
> 
>  - Chuck
> 
> 
> THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
> MATERIAL and is thus for use only by the intended recipient. If you
> received this in error, please contact the sender and delete the e-mail
> and its attachments from all computers.
> 
> 
> 

-- 
View this message in context: http://old.nabble.com/set-cookie-tp28639362p28641797.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


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


RE: set-cookie

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: André Warnier [mailto:aw@ice-sa.com]
> Subject: Re: set-cookie
> 
>     //  Store products in the ServletContext
> 	getServletContext().setAttribute("products", products);
> 
> Presumably, if you store something in the context, it is because you
> want to retrieve it later (or else, what's the point he ?).
> Now, Tomcat has no way to know when you'll come back to retrieve it, so
> it has to create a session to store this.

Nope, that's not it (but I don't know what is, yet).  Attributes stored in a ServletContext have a webapp, not session, scope; no session is needed here, and nothing needs to be communicated between the client and server to retrieve them.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.


Re: set-cookie

Posted by André Warnier <aw...@ice-sa.com>.
banto wrote:
> Hi gurus,
> 
> my problem here is that i'm trying to understnad a servlet code that makes
> tomcat to send the
> 
> Set-Cookie: JSESSIONID=8BCB60D6F6B9394B4ABD2FDD007BDB39; Path=/store
> 
> header.
> Now the code is something like:
> 
> public class FrontController extends HttpServlet {
> 
> 	public void init() throws ServletException {
> 
> 		HashMap products = new HashMap();
> 		Product p = new Product(1, "Dog", "9.99");
> 		products.put("1", p);
> 		//  Store products in the ServletContext
> 		getServletContext().setAttribute("products", products);
> 	}
> 	public void doPost(HttpServletRequest request, HttpServletResponse
> response)
> 			.....
> 
> and the web.xml is quite standard. 
> my problem is that i don´t see where the servlet is instructing tomcat to
> sent that header.
>
I would guess right here :
    //  Store products in the ServletContext
	getServletContext().setAttribute("products", products);

Presumably, if you store something in the context, it is because you 
want to retrieve it later (or else, what's the point he ?).
Now, Tomcat has no way to know when you'll come back to retrieve it, so 
it has to create a session to store this.  And to allow you to get that 
same session data back later (and not someone else's data), it has to 
create some unique key for it.  And if you have to be able to get that 
data later, you need this key.  So Tomcat is nice and sends it to you, 
inside of a cookie. This way, you do not need to remember to send the 
key back when you next come around, your browser will do it 
automatically for you.
Neat, he ?


> My other servlets are not issuing that header when running. So what i´m
> missing?

Probably, because they do not store anything in their context.


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