?
Solved

displaying two cookies

Posted on 2005-03-15
9
Medium Priority
?
253 Views
Last Modified: 2010-03-31
can someone explain to me how to set two cookies. i think i have it set correct but it displays not how i would like it too...
i have an html form for a user to fill out (works fine)
the html form calls a WriteNewsCookie servlet to create cookies (this is working)
the WriteNewCookie redirects to the EcapNewsService (and this works)
but my display shows the first cookie twice and then the second cookie twice...

here is my html and my two java files...
basically i want a user to open this URL http://matrix.csis.pace.edu:23003/it608/servlet/week08.EcapNewsService
and if they are new users it will redirect them to the html form..if they are repeat users it displays the cookies

any suggestions on how to get the cookies to display properly...like

Hello, COOKIE_USERNAME.
Here is the latest COOKIE_INTEREST news.

and not have it repeat like it does??????

***********HTML***************
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<HTML>
    <HEAD>
        <TITLE>Welcome to Ecap News Site</TITLE>
    </HEAD>
       
    <body bgcolor="#00FF00" text="#000000">
   
    <div align="center">
    <h3>Welcome to Ecap News Site</h3>
   
    <form name="enrol" method="get" action="http://matrix.csis.pace.edu:23003/it608/servlet/week08.WriteNewsCookie" > 

    <table width="350" border="1" cellpadding="1">
        <tr>
        <td width="100">User Name</td>
        <td width="250">
            <input type="text" name="username">
        </td>
        </tr>
       
    <tr>
        <td colspan="2">Please fill out this form and press submit so that we
        can bring the news you want.</td>
    </tr>
   
    <tr>
        <td width="100">Primary Interest</td>
        <td width="250">
            <input type="radio" name="interest" value="national">National News&nbsp;&nbsp;
            <input type="radio" name="interest" value="local" checked>Local News&nbsp;&nbsp;
            <input type="radio" name="interest" value="sports">Sports News&nbsp;&nbsp;
            <input type="radio" name="interest" value="entertainment">Entertainment News&nbsp;&nbsp;
        </td>
    </tr>
   
    <tr>
        <td width="100">&nbsp;</td>
        <td width="250">&nbsp;</td>
    </tr>
   
    <tr>
        <td width="100">&nbsp;</td>
        <td width="250">
            <input type="submit" name="Set Preferences" value="Submit">
            <input type="reset" name="Reset" value="Reset">
        </td>
    </tr>
   
    </TABLE>
    </div>
    </FORM>

    </BODY>
</HTML>


*********************JAVA1**********************
package week08;

import java.io.* ;
import javax.servlet.* ;
import javax.servlet.http.* ;
import coreservlets.*;

public class WriteNewsCookie extends HttpServlet
{

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        Cookie myCookie;//cookie username
        Cookie myCookie1;//cookie interest
        PrintWriter out;  
        String title;
        String username;
        String interest;
        String value;
        int life;

        response.setContentType( "text/html" );
        out = response.getWriter();
       
        username = "blankname";
        if(request.getParameter("username") != null)
        {
            username = request.getParameter("username");
        }//end if
       
        interest = "blankinterest";
        if (request.getParameter("interest") != null)
        {
            interest = request.getParameter("interest");
        }//end if
       
        value = "blankvalue";
        if (request.getParameter("value") != null)
        {
            value = request.getParameter("value");
        }//end if
       
        life = (60 * 60 * 24 * 7);//one week life
       
        try
        {
            life = Integer.parseInt(request.getParameter("life")) * 60;
        }//end if
        catch (NumberFormatException e)
        {
            //leave life as one week
        }//end catch

        myCookie = new Cookie(username, value);
        myCookie.setMaxAge(life);
        response.addCookie(myCookie);
       
        myCookie1 = new Cookie(interest, value);
        myCookie1.setMaxAge(life);
        response.addCookie(myCookie1);
       
        response.sendRedirect("http://matrix.csis.pace.edu:23003/it608/servlet/week08.EcapNewsService");
       
        /*title = "Setting My News Cookies";
        out.println (ServletUtilities.headWithTitle( title ) +
                "<BODY BGCOLOR=\"#FF00FF\">\n" +
                "<H1 ALIGN=\"CENTER\">" + title + "</H1>\n" +
                "You created two cookies with this page.\n" +
                "View the User Input! \n" +
                "<A HREF=\"http://matrix.csis.pace.edu:23003/it608/servlet/week08.EcapNewsService\"> \n" +
                //"<A HREF=\"http://localhost:8080/it608/servlet/week08.EcapNewsService\"> \n" +
                "<CODE>Display EcapNewsService</CODE> servlet</A>.\n" +
                "<P>\n" +
                "</BODY></HTML>"); */
               
        out.close();
    }//end of doGet
}//end WriteNewsCookie class

*************************JAVA2**********************
package week08;

import java.io.* ;
import javax.servlet.* ;
import javax.servlet.http.* ;
import coreservlets.*;


public class EcapNewsService extends HttpServlet
{

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        PrintWriter out;  
        long currentTime;
        long oneSecond;
       
        response.setContentType( "text/html" );
        out = response.getWriter();
       
        currentTime= System.currentTimeMillis();
        oneSecond = 1000;
        response.setDateHeader("Expires" , currentTime + oneSecond);

        String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
        "Transitional//EN\">\n";
     
        String title = "Welcome to your special news page!";

        out.println(ServletUtilities.headWithTitle(title) +
                "<BODY BGCOLOR=\"#FFFF00\">\n" +
                "<H1 ALIGN=\"CENTER\">" + title + "</H1>");
               
                boolean newbie = true;
               
                Cookie[] cookies = request.getCookies();
                if(cookies != null)
                {
                    for(int i = 0; i < cookies.length ; i++)
                    {
                        Cookie cookie = cookies[i];
                        if((cookie.getName().equals("repeatVistor")) && (cookie.getValue().equals("yes")))
                        {
                            newbie = false;
                            break;
                        }//end if
                        if(newbie)
                        {
                            Cookie returnVistorCookie = new Cookie("repeatVistor", "yes");
                            returnVistorCookie.setMaxAge(60 * 60 * 24 * 7);//one week
                            response.addCookie(returnVistorCookie);
                           
                            out.println("Hello,  " + cookie.getName() + ".");
                            out.println("<br>");
                            out.println("Here is the lastest " + cookie.getName() + " news.");    
                            out.println("<br>");
                        }//end if
                    }//end for
                }//end if
                else
                {
                    response.sendRedirect("http://matrix.csis.pace.edu/~s05-it608-s02/homework/lesson08/customize.html");  
                }//end else
               
                out.println("<br> \n" +
                "</BODY></HTML>");
               
        out.close();
    }//end of doGet
}//end EcapNewsService class
0
Comment
Question by:annie613
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 6

Expert Comment

by:durgaprasad_j
ID: 13555411
hi,

1) in EcapNewsService servlet, in the for loop, when you are checking, if you find it is not a repeatVisitor cookie, you are immediately creating a new cookie. IMO that is wrong. First you should check for all cookies and then decide whether he is a repeatVisitor or not. check below.

                Cookie[] cookies = request.getCookies();
                if(cookies != null)
                {
                    for(int i = 0; i < cookies.length ; i++)
                    {
                        Cookie cookie = cookies[i];
                        if((cookie.getName().equals("repeatVistor")) && (cookie.getValue().equals("yes")))
                        {
                            newbie = false;
                            break;
                        }//end if
                        if(newbie)
                        {
                            Cookie returnVistorCookie = new Cookie("repeatVistor", "yes");
                            returnVistorCookie.setMaxAge(60 * 60 * 24 * 7);//one week
                            response.addCookie(returnVistorCookie);
                           
                            out.println("Hello,  " + cookie.getName() + ".");
                            out.println("<br>");
                            out.println("Here is the lastest " + cookie.getName() + " news.");    
                            out.println("<br>");
                        }//end if
                    }//end for
                }//end if

Should be

Cookie[] cookies = request.getCookies();
                if(cookies != null)
                {
                    for(int i = 0; i < cookies.length ; i++)
                    {
                        Cookie cookie = cookies[i];
                        if((cookie.getName().equals("repeatVistor")) && (cookie.getValue().equals("yes")))
                        {
                            newbie = false;
                            break;
                        }//end if
                     }//end for
                        if(newbie)
                        {
                            Cookie returnVistorCookie = new Cookie("repeatVistor", "yes");
                            returnVistorCookie.setMaxAge(60 * 60 * 24 * 7);//one week
                            response.addCookie(returnVistorCookie);
                           
                            out.println("Hello,  " + cookie.getName() + ".");
                            out.println("<br>");
                            out.println("Here is the lastest " + cookie.getName() + " news.");    
                            out.println("<br>");
                        }//end if
                   
                }//end if

2) How are you checking it on your browser??

 Are you deleting the previous cookies when you are testing  ?

Hope this helps
DP
0
 

Author Comment

by:annie613
ID: 13555811
thanks for the comments...i am deleting the previous cookies when i am testing so i know they are working...it was the display i was having trouble with...im going to implement some of the above code and see how it works
0
 

Author Comment

by:annie613
ID: 13555840
since you end the for loop before using cookie.getName it doenst recongize the variable name cookie when it compiles...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 6

Expert Comment

by:durgaprasad_j
ID: 13555920
hi,
     what iam saying is you are creating cookie there multiple times.  If you want to print that statements also, then create a cookie and print their names "Hello blah.. " , After this put break; to come out of forloop.

          Cookie[] cookies = request.getCookies();
                if(cookies != null)
                {
                    for(int i = 0; i < cookies.length ; i++)
                    {
                        Cookie cookie = cookies[i];
                        if((cookie.getName().equals("repeatVistor")) && (cookie.getValue().equals("yes")))
                        {
                            newbie = false;
                            break;
                        }//end if
                        if(newbie)
                        {
                            Cookie returnVistorCookie = new Cookie("repeatVistor", "yes");
                            returnVistorCookie.setMaxAge(60 * 60 * 24 * 7);//one week
                            response.addCookie(returnVistorCookie);
                           
                            out.println("Hello,  " + cookie.getName() + ".");
                            out.println("<br>");
                            out.println("Here is the lastest " + cookie.getName() + " news.");    
                            out.println("<br>");

                             break;

                        }//end if
                    }//end for
                }//end if
0
 

Author Comment

by:annie613
ID: 13556028
                       if(newbie)
                        {
                            Cookie returnVistorCookie = new Cookie("repeatVistor", "yes");
                            returnVistorCookie.setMaxAge(60 * 60 * 24 * 7);//one week
                            response.addCookie(returnVistorCookie);
                           
                            out.println("Hello,  " + cookie.getName() + ".");
                            out.println("<br>");
                            out.println("Here is the lastest " + cookie.getName() + " news.");    
                            out.println("<br>");

                             break;

                        }//end if

but i still dont see how this code compiles...
the cookie.getName() cannot resolve symbol: variable cookie in class week08.EcapNewsService

0
 

Author Comment

by:annie613
ID: 13558218
i think my main problem is that when the cookie is sent to the page to display it works well..and im not having trouble with it...but if i go back to the HTML form and enter in new information and pass it to the WriteNewsCookie it redirects to the EcapNewsService but displays the first cookie I created...

How can i get the cookies to override each other????
0
 
LVL 6

Expert Comment

by:durgaprasad_j
ID: 13563239
hi, i think there is not direct way for overriding cookies each other.

one way would be to find the cookie and delete it and create a cookie with the same name again.
0
 
LVL 6

Accepted Solution

by:
durgaprasad_j earned 800 total points
ID: 13563496
hi,
     suppose if there is a cookie by name "dp", get that cookie and set the maxage of that to 0, then that will be deleted.
    cookieobject.setMaxAge(0); //now you have deleted that cookie
    create a new cookie with that name again and add to the response like reponse.addCookie(cookieobjectnew);

   Try this first, i saw your post on the same question in this section, but it would be better to create a link in html section pointing to that/this question.
0
 

Author Comment

by:annie613
ID: 13564577
thanks for all your insight....by deleting the cookie
>>cookieobject.setMaxAge(0); //now you have deleted that cookie
i was able to get all the code to work properly!
cheers :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month9 days, 13 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question