Trying to use cookies with a JSP...

T

Tim O'Connor

....and not having a lot of success. I'm using this code that I got
from a website:

<%
Cookie[] cookies = request.getCookies();

for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++)
{
Cookie cookie1 = cookies[loopIndex];
out.println("name = " + cookie1.getName() + "<BR>");
out.println("value = " + cookie1.getValue() + "<BR>");
out.println("domain = " + cookie1.getDomain() + "<BR>");
}
%>

When I run it, it retrieves a single cookie whose name is SITESERVER,
value is ID=averylonghexnumber and Domain is null. This does not
resemble any of the cookies in my C:\Documents and
Settings\OConnorT\Cookies directory (of which there are many). I've
also tried setting a cookie using:
<%
Cookie cookie = new Cookie("MyCookie", "CookieValue");
response.addCookie(cookie);
%>

but when I read it back it can't be found. What am I doing wrong?
Does it have to do with the cookie Domain? What is the "domain" of a
cookie? I'm using Windows XP Professional and IE 6.xx.

Tim
 
S

Scott Yanoff

Tim said:
<%
Cookie[] cookies = request.getCookies();

for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++)
{
Cookie cookie1 = cookies[loopIndex];
out.println("name = " + cookie1.getName() + "<BR>");
out.println("value = " + cookie1.getValue() + "<BR>");
out.println("domain = " + cookie1.getDomain() + "<BR>");
}
%>

It might be worthwhile to wrap that "for" loop with the following:
if (cookies != null).

It's often faster to loop backwards rather than calculating
cookies.length with each iteration of the loop. I doubt you save many
milliseconds doing it this way, but ya never know.
for (int loopIndex = cookies.length - 1; loopIndex >= 0; loopIndex--) {

Lastly, are you aware that you cannot read the cookies on your
harddrive. Instead, you are reading the cookies of the user's browser
that is hitting your JSP. And, you can only read cookies that you
created from your domain, for obvious security reasons.
Settings\OConnorT\Cookies directory (of which there are many). I've
also tried setting a cookie using:
<%
Cookie cookie = new Cookie("MyCookie", "CookieValue");
response.addCookie(cookie);
%>

It's usually a good idea to set some other things associated with the
cookie, such as expires, path, domain, etc. For example, you would want
to set the path so that the cookie can be read from any part of your
site. You can limit where the cookie can be accessed if you change
this, but generally cookies get set to:
cookie.setPath("/");

You can limit which machine in your domain the cookie is valid for, or
specify that it is valid for anything in ".timoconnor.org" with:
cookie.setDomain(".timoconnor.org");

HTH,
 
P

Phil Hanna

...and not having a lot of success. I'm using this code that I got
from a website:

<%
Cookie[] cookies = request.getCookies();

for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++)
{
Cookie cookie1 = cookies[loopIndex];
out.println("name = " + cookie1.getName() + "<BR>");
out.println("value = " + cookie1.getValue() + "<BR>");
out.println("domain = " + cookie1.getDomain() + "<BR>");
}
%>

When I run it, it retrieves a single cookie whose name is SITESERVER,
value is ID=averylonghexnumber and Domain is null. This does not
resemble any of the cookies in my C:\Documents and
Settings\OConnorT\Cookies directory (of which there are many). I've
also tried setting a cookie using:
<%
Cookie cookie = new Cookie("MyCookie", "CookieValue");
response.addCookie(cookie);
%>

but when I read it back it can't be found. What am I doing wrong?

If you want a cookie to persist between browser sessions, you need to
set its expiration date to a non-zero value. By default, it goes away
as soon as the HTTP session does.

final int ONE_DAY = 60 * 60 * 24; // Seconds in one day

Cookie cookie = new Cookie("MyCookie", "CookieValue");
cookie.setMaxAge(ONE_DAY);
response.addCookie(cookie);

Also, all those cookies in your browser cache are ignored because they
don't belong to the same domain as the page that's reading them.

The servlet API javadocs for javax.servlet.http.Cookie describe the
domain and path parameters you may want to adjust.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,838
Latest member
KandiceChi

Latest Threads

Top