File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes basic cookie Doubts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "basic cookie Doubts" Watch "basic cookie Doubts" New topic

basic cookie Doubts

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I want the system to remember the user. So I have to create a Cookie. How to write the code so that it will remember the value?
How will the server know that he is entering it for the first time?
Now how does the server remember the name of the user?
Please help me I am new to this cookie concept. Can u give me a small example.

William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13036
It is more than just the "system remembering the user" - the user (web browser) must remember the system. If the user's browser has a cookie with your URL and some other conditions, the information will be automatically included with any request from the browser.
Your web server/servlet engine will parse this information and put it where your servlet can get it with the HttpServletRequest getCookies method.
If this method returns null either this is the first time the user has visited this URL OR the cookie expired OR the user's browser is not keeping cookies.
It is up to you to put some unique ID in a cookie and figure out some way to associate that ID with the user. It is also up to you to set the cookie parameters for how long the cookie will live, and the URL it will be associated with, etc.
As far as creating a cookie is concerned, here is a chunk of
code from a chat room servlet that makes two cookies and attaches them to a HttpServletResponse.
Cookie hC = new Cookie( "handle", handle );
hC.setMaxAge( 600 ); hC.setVersion( 1 );
Cookie rC = new Cookie( "room", room );
rC.setMaxAge( 600 ); rC.setVersion( 1 );
resp.addCookie( hC );
resp.addCookie( rC );

nag Guna

Joined: Oct 27, 2005
Posts: 1
Hello friend u can go through "Servlet Programming" by Oreilly.
U can refer Shopping Cart example for creating Cookies,adding them to browser and retrieving.U can get a complete idea of cookies if u go through these examples at different stages.
But using Cookies is not preferable when u develop a servlet.Because cookies will not allowed or restricted by some of the routers while they travel through the client to server.Better u concentrate on builtin HttpSession tracking.
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanx for all ur suggestion. But i want to know how do u identify that the user is coming for the first time thru code. ??
I am still not clear.
Tom P
Ranch Hand

Joined: May 06, 2000
Posts: 88
A first time user won't have a cookie!
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

I have done this but I get a NullPointerException and It goes
into the if loop which says "if (cookies!=null)". What is the problem? . And displays cookie[0].getValue() in the text box. Please correct my code.
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

Ignore the previos post.
I got the code working but it displays JavaUser in my textbox.
And It does'nt save the uid i enter and submit. Every time I refresh it displays JavaUser as the value. From where is this JavaUser coming. I don't understand.. I still get NullPointerException. Please correct my code.

I agree. Here's the link:
subject: basic cookie Doubts
It's not a secret anymore!