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." +
"</form></body></html>");
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." +
"<input type=\"hidden\" name=\"sw\" value=\"y\" />" +said:" + "<br />" + "</p>" +
"</form></body></html>");