Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / July 2007

Tip: Looking for answers? Try searching our database.

Cookie Help

Thread view: 
Dan - 06 Jul 2007 04:05 GMT
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[i];
                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." +
        "<br />" + "<input type=\"submit\" name=\"SButton\" value=\"Send\" /
>" + "<br />" + "</p>" +
        "<input type=\"hidden\" name=\"sw\" value=\"y\" />" +
        "</form></body></html>");
Andrew Thompson - 06 Jul 2007 08:09 GMT
>Okay, another question....
>I am programming cookies for the first time.
...
>Below is my code - what am I doing wrong?

The first thing you are doing wrong is debugging
a servlet/JSP rather than making a pure HTML/JS
solution, validating the HTML (and the JS if
applicable) and debugging that.

Once that works correctly, it will be much easier
to make it work correctly in J2EE code.

It might help also, when asking questions about
the HTML/JS version (on an appropriate group) to
state the behaviour you expect, and the actual
behaviour observed.

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Roedy Green - 07 Jul 2007 07:32 GMT
>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.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.