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

New to cookies

varkala prabhakar
Ranch Hand

Joined: Sep 08, 2001
Posts: 54
Hai all,
In my servlet Program i use cookies and i am setting them using

Cookie cook = new Cookie(id,login);
response.addCookie(cook);
its working fine and when i retrive cookies in the other program using
Cookie []cook = request.getCookies();
i get the cook.length Zero and my servlet is not working and i get a null point exception.saying
java.lang.NullPointerException
at ShowCategory.doGet(ShowCategory.java:43)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:499)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:588)
at sun.servlet.http.HttpServerHandler.sendResponse(HttpServerHandler.java:165)
at sun.servlet.http.HttpServerHandler.handleConnection(HttpServerHandler.java:121)
at sun.servlet.http.HttpServerHandler.run(HttpServerHandler.java:90)
at java.lang.Thread.run(Unknown Source)

Could anyone help me how to retrive my cookie values.
thanx
prabhakar
Marcos Maia
Ranch Hand

Joined: Jan 06, 2001
Posts: 977
Check if your browser is not configured to don�t accept Cookies, you should also notice that by default a cookie only persists until browser shutdown, you can use the method setMaxAge(int expiry) wich sets the maximum age of the cookie in seconds, to change this behavior.
Also take a look at http://java.sun.com/docs/books/tutorial/servlets/client-state/cookies.html there you�ll find more information on Cookies.
regards.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

The NullPointerException is because your getCookies() method will return null if there are no cookies.

When you then attempt to get the length of the cook array, 'cook' has been initialized with the null returned by getCookies() and so you throw a NullPointerException.

To prevent this, (and this is useful in *many* other areas of java programming), always check for null first before using that variable.
[This message has been edited by Mike Curwen (edited October 19, 2001).]
varkala prabhakar
Ranch Hand

Joined: Sep 08, 2001
Posts: 54
dear curwen,
thanx for you reply curwin and what you have said is perfectly right but in my cookies initialization program , setting cookie works fine but at the time of retrival i get the null exception. i dont know where the cookie is gone. and i have even set the cookie time as xxx.setMaxAge(10000).
thanx
prabhakar.
varkala prabhakar
Ranch Hand

Joined: Sep 08, 2001
Posts: 54
Hi,
My code is some thing like this

Cookie []cook = request.getCookies();
System.out.println(" Category ="+cat + " randum number:"+ randnum );
if(cook.length == 0)
{
System.out.println("is null ");
}
for(int i = 0; i < cook.length;i++){
if( cook[i].getName().equals(merid) ){
rand = (String)cook[i].getValue() ;
}
}
---------------------------------------
request.getCookies() works fine, i get the null exception in cook.length it get the value as null.

thanx
prabhakar.
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Like Mike Curwin already said, your request.getCookies() is returning null. It's either cookies are not allowed by the client or you are not specifying your cooke name correctly. You are getting the exception whe you try to call the length variable on a null array reference.

Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

request.getCookies() does *not* work fine. You are getting a null. When you try to access a field (.length) of a non-existent object, you get a NullPointerException. This is why you must *always* check an object for null before you attempt to access a field or method of that object. Do that, or pre-initialize the object to some state, other than null.

So as Bosun has said: either your setCookie() doesn't work like you think it does, or your cookie name is wrong. The third thought I had was your servlet that gets cookies is operating under a different domain than the one that sets your cookies.
raj sekhar
Ranch Hand

Joined: Oct 16, 2001
Posts: 117
hey
Just modify you if condition to check null values of 'cook' too besides checking its length to zero...and you will know whats going on...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: New to cookies