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

help with cookie

aryan sahu
Greenhorn

Joined: Oct 12, 2011
Posts: 11

hello, i am very much confused with this cookie thing.For state management HttpSession objects are created in containers and a session id is assigned to it.
I understand this is used to identify the client request , i mean i am confused if session id's are encapsulated in cookies and are passed to client in response.

does this statement creates a cookie the background

HttpSession s=request.getSession();

or Cookie ck=new Cookie("name",value); i am confused...help!
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Basically cookies are used to store simple text information on the client which gives the server some a-priori information about the client when a request comes in.
Like the "remember me on this computer" checkbox or your language preference for that site when you access it from the same machine and same browser.
Session Id is one such information that is passed through cookies provided the client browser has not disabled them.

Now to answer your original questions
aryan sahu wrote:
does this statement creates a cookie the background

HttpSession s=request.getSession();

or Cookie ck=new Cookie("name",value);


Statement 1: Yes. It does create new cookie which will store the session id but only after the response is given back to the client.

Statement 2: No. This cookie is not created on the browser until you say response.addCookie(ck); assuming response is your servlet's response object.

Hope this helps


SCJP, SCWCD.
|Asking Good Questions|
abani patra
Ranch Hand

Joined: Oct 11, 2011
Posts: 70
Hi,
I agree with Amit Ghorpade.
I want to just add that with the first time request the cookie will not be in request after onward on every request you can have the cookie value in your request.
Aniket S. Kulkarni
Ranch Hand

Joined: Jun 15, 2011
Posts: 87

The below links may help you.

Cookie
session


Aniket Kulkarni
Oracle Certified Professional, Java SE 6 Programmer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help with cookie