Cookie Help

D

Dan

Okay, another question....
I am programming cookies for the first time. I have three links that
my servlet goes to (nytimes.com, washingtontimes.com, tribune.com) and
I'm supposed to have three cookies that keep track of this.....

First I setup the three cookies, then I do a simple req.getParameter
from the radio buttons to check to see what is being passed, and I add
the cookie then.

Below is my code - what am I doing wrong?

Cookie n = new Cookie("nyc", "0");
n.setMaxAge(60*60*24*7);
Cookie w = new Cookie("wash","0");
w.setMaxAge(60*60*24*7);
Cookie t = new Cookie("trib","0");
t.setMaxAge(60*60*24*7);
if(req.getParameter("Pages").equals("Wash"))
{
resp.addCookie(w);
//resp.sendRedirect("http://www.washingtonpost.com");
}
if(req.getParameter("Pages").equals("Nytimes"))
{
resp.addCookie(n);
//resp.sendRedirect("http://www.nytimes.com");
}
if(req.getParameter("Pages").equals("Trib"))
{
resp.addCookie(t);
//resp.sendRedirect("http://www.chicagotribune.com");
}
Cookie[] cookies = req.getCookies();
if(cookies != null)
{
for(int i=0;i<cookies.length;i++)
{
Cookie name = cookies;
if(name.getName().equals("wash"))
{
int countw=Integer.parseInt(w.getValue());
countw++;
w.setValue(Integer.toString(countw));
}
if(name.getName().equals("nyc"))
{
int countn=Integer.parseInt(n.getValue());
countn++;
n.setValue(Integer.toString(countn));
}
if(name.getName().equals("trib"))
{
int countt=Integer.parseInt(t.getValue());
countt++;
t.setValue(Integer.toString(countt));
}
}
}
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.println("<html>" +
"<head><title>" +
"DirectIt2Servlet.html" + "</title></head>" +
"<body>" +
"<form action=\"/MyWebApp/DirectIt2Servlet\">" +
"<p>" + "Below are three choices for three different web pages that
you can go to. Click on the radio button, and then click on submit to
go to that page." + "<br />" +
"<br />" + "http://www.washingtonpost.com" +
"<input type=\"radio\" name=\"Pages\" value=\"Wash\" />" +
"<br />" + "The Washington Times has been accessed " + w.getValue()
+ " times since " + "June 26, 2007." +
"<br />" + "http://www.nytimes.com <input type=\"radio\" name=\"Pages
\" value=\"Nytimes\" />" +
"<br />" + "The NY Times has been accessed " + n.getValue() + "
times since " + "June 26, 2007." +
"<br />" + "http://www.chicagotribune.com <input type=\"radio\" name=
\"Pages\" + value=\"Trib\" />" +
"<br />" + "The Chicago Tribune has been accessed " + t.getValue() +
" times since " + "June 26, 2007." +
said:
" + "<br />" + "</p>" +
"<input type=\"hidden\" name=\"sw\" value=\"y\" />" +
"</form></body></html>");
 
R

Roedy Green

Below is my code - what am I doing wrong?

to answer that question, you want to see what sort of cookie your
browser keeps when you do this manually.
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top