Cookie Counts

Discussion in 'Java' started by Dan, Jul 22, 2007.

  1. Dan

    Dan Guest

    Hello -

    I'm having a hard time displaying how many times a site has been
    accessed using cookies.

    For example, one of the sites that my servlet is redirecting to is
    nytimes.com

    In my code I have:
    public class DirectIt2Servlet extends HttpServlet
    {
    int countn=1;
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException
    {
    Cookie[] cookies = req.getCookies();
    if(cookies==null) //adds cookie if null
    {
    Cookie n = new Cookie("nyc", "" + countn);
    n.setMaxAge(60*60*24*7);
    resp.addCookie(n);
    }
    if(req.getParameter("sw")==null) //checks hidden value to see if
    first time through
    {
    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 />");
    for(int j=0;j<cookies.length;j++) //checks the cookies
    {
    Cookie temp;
    temp=cookies[j];
    if(temp.getName().equals("nyc"))
    {
    out.println("<br />" + "http://www.nytimes.com");
    out.println("<input type=\"radio\" name=\"Pages\" value=\"Nytimes
    \" />");
    out.println("<br />" + "The NY Times has been accessed " +
    temp.getValue() + " times since " + "//(beginning of count date//");
    }
    }
    out.println("<br />" + "<input type=\"submit\" name=\"SButton\" value=
    \"Send\" />" + "<br />" + "</p>" +"<input type=\"hidden\" name=\"sw\"
    value=\"y\" />" + "</form></body></html>"); }
    else (if hidden value is not equal to null)
    {
    if(req.getParameter("Pages").equals("Nytimes")) //add the cookies
    {
    Cookie nyccount = new Cookie("nyc", "" + countn++);
    resp.addCookie(nyccount);
    resp.sendRedirect("http://www.nytimes.com");
    }

    How do I put in the count date? Am I doing everything correctly? HELP!
     
    Dan, Jul 22, 2007
    #1
    1. Advertising

  2. On Jul 21, 5:18 pm, Dan <> wrote:
    > Hello -
    >
    > I'm having a hard time displaying how many times a site has been
    > accessed using cookies.
    >
    > For example, one of the sites that my servlet is redirecting to is
    > nytimes.com
    >
    > In my code I have:
    > public class DirectIt2Servlet extends HttpServlet
    > {
    > int countn=1;
    > public void doGet(HttpServletRequest req, HttpServletResponse resp)
    > throws ServletException, IOException
    > {
    > Cookie[] cookies = req.getCookies();
    > if(cookies==null) //adds cookie if null
    > {
    > Cookie n = new Cookie("nyc", "" + countn);
    > n.setMaxAge(60*60*24*7);
    > resp.addCookie(n);
    > }
    > if(req.getParameter("sw")==null) //checks hidden value to see if
    > first time through
    > {
    > 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 />");
    > for(int j=0;j<cookies.length;j++) //checks the cookies
    > {
    > Cookie temp;
    > temp=cookies[j];
    > if(temp.getName().equals("nyc"))
    > {
    > out.println("<br />" + "http://www.nytimes.com");
    > out.println("<input type=\"radio\" name=\"Pages\" value=\"Nytimes
    > \" />");
    > out.println("<br />" + "The NY Times has been accessed " +
    > temp.getValue() + " times since " + "//(beginning of count date//");
    > }
    > }
    > out.println("<br />" + "<input type=\"submit\" name=\"SButton\" value=
    > \"Send\" />" + "<br />" + "</p>" +"<input type=\"hidden\" name=\"sw\"
    > value=\"y\" />" + "</form></body></html>"); }
    > else (if hidden value is not equal to null)
    > {
    > if(req.getParameter("Pages").equals("Nytimes")) //add the cookies
    > {
    > Cookie nyccount = new Cookie("nyc", "" + countn++);
    > resp.addCookie(nyccount);
    > resp.sendRedirect("http://www.nytimes.com");
    > }
    >
    > How do I put in the count date? Am I doing everything correctly? HELP!


    If you are doing a response.sendRedirect to another site, I doubt that
    cookies from your site will be set in the user's browser. You will
    have to design the system in a way that there is an intermediate
    action between the cookie setting and the redirection - maybe a client
    redirection instead of a server redirect.

    -cheers,
    Manish
     
    Manish Pandit, Jul 22, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmFtZXNT?=

    ASP.NET Worker Process Thread Counts

    =?Utf-8?B?SmFtZXNT?=, Apr 27, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    668
    =?Utf-8?B?SmFtZXNT?=
    Apr 27, 2005
  2. R
    Replies:
    7
    Views:
    926
    =?ISO-8859-1?Q?J=FCrgen_Kahrs?=
    Jan 17, 2005
  3. Steven Bethard

    selecting dictionaries to maximize counts

    Steven Bethard, Feb 18, 2005, in forum: Python
    Replies:
    5
    Views:
    307
    Brian Beck
    Feb 19, 2005
  4. raghu

    Reference Counts

    raghu, May 18, 2006, in forum: Python
    Replies:
    5
    Views:
    456
    Tim Peters
    May 19, 2006
  5. Replies:
    0
    Views:
    355
Loading...

Share This Page