Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

Setting and Reading a Cookie in the Same Page

The previous example set a cookie in one page and read it in another, but sometimes you might want to set and read a cookie in the same page. For example, if your home page allows the user to customize some aspects of the page, such as its background color, you can store that information in a cookie. The next time the user opens your home page, your code can read that cookie and display the correct background color.

Here's an example to show how this works—in this case, the code will set a cookie named color to cyan, and the next time you load the same page, the JSP code will use that cookie to change the background color of the page to cyan. When the page loads, it will check for the cookie named color, and if it's found, use its value to set the background color of the Web page. However, if that cookie isn't found, the code will set a Boolean variable named foundCookie to false, which means it will have to create the cookie:

<HTML>
  <HEAD>
    <TITLE>Setting and Reading Cookies</TITLE>
  </HEAD>
 
  <BODY
    <%
    Cookie[] cookies = request.getCookies();
    boolean foundCookie = false;

    for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) { 
      Cookie cookie1 = cookies[loopIndex];
      if (cookie1.getName().equals("color")) {
        out.println("bgcolor = " + cookie1.getValue());
        foundCookie = true;
      }
    } 
    .
    .
    .

If foundCookie is false the code will set the color cookie. You can test if foundCookie is false with an if statement and the JSP not operator. ! (discussed in Day 2), which flips the Boolean sense of its argument like this

if(!foundCookie)

where the body of the if statement is executed if the color cookie wasn't found. In that case, the code creates the cookie and stores it as you see in Listing 7.4.

Listing 7.4 Setting and Reading Cookies in the Same Page (ch07_04.jsp)

<HTML>
  <HEAD>
    <TITLE>Setting and Reading Cookies</TITLE>
  </HEAD>
 
  <BODY
    <%
    Cookie[] cookies = request.getCookies();
    boolean foundCookie = false;

    for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) { 
      Cookie cookie1 = cookies[loopIndex];
      if (cookie1.getName().equals("color")) {
        out.println("bgcolor = " + cookie1.getValue());
        foundCookie = true;
      }
    } 

    if (!foundCookie) {
      Cookie cookie1 = new Cookie("color", "cyan");
      cookie1.setMaxAge(24*60*60);
      response.addCookie(cookie1); 
    }
    %> 
    >
    <H1>Setting and Reading Cookies</H1>
    This page will set its background color using a cookie.
  </BODY>
</HTML>

When you load this page the first time, it sets the color cookie, and the page background will be white. When you load the page from then on, until the cookie expires, the page reads that cookie and uses it to turn the page background cyan, as you see in Figure 7.5.

Figure 7.5Figure 7.5 Setting and reading a cookie using the same page.

That gives you a good foundation on how to use cookies. You've seen how to set cookies and read them back in, using separate pages or the same page. You've seen how to specify how long the cookie should exist, what its name should be, and what its value should be. As you can see, cookies are great for tracking individual users—after all, you're storing data on the user's computer; how much more tracking do you need?

It turns out that you might indeed need more tracking—you might want to preserve the data in various variables in your code between user accesses to the page. Cookies can store data as text on the user's computer for you, but what if you want to store your data in objects—and have those objects retain their value between page loadings? For something such as that, you can use sessions.

  • + Share This
  • 🔖 Save To Your Account