Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

email article
printer friendly

Check the User's Cookie Setting
Don't assume the user's browser will accept cookies. Here's a trick to help you check.
by Budi Kurniawan

Posted April 23, 2002

Even though all Web browsers I know of leave the shop with the cookie setting on, the user has the discretion to turn it off, sometimes doing so by accident. When you use cookies in your servlets/JSP pages, you can't assume the user's browser will accept them—always check first. Here's a trick to check whether the user's browser's cookie setting is enabled.

The trick is simple. You send an HTTP response from your servlet or JSP page that forces the browser to come back for the second time. With the first response, you send a cookie. When the browser comes back for the second time, you check if the request carries the cookie sent previously. If the cookie is there, you can conclude that the browser setting for cookies is on. Otherwise, the user could be using an old browser that doesn't recognize cookies at all, or the cookie support for that browser is off.

This JSP page uses this trick to check if the cookie setting is enabled. If the setting is enabled, the page sends the string, "Cookie is on". Otherwise, it sends the string, "Cookie is off":

<%
  if (request.getParameter("flag")==null) {
    // the first request
    Cookie cookie = new Cookie("cookieSetting", 
"on");
    response.addCookie(cookie);
    String nextUrl = request.getRequestURI() + 
"?flag=1";
    // force the browser to refresh
    out.println("<META HTTP-EQUIV=Refresh 
CONTENT=0;URL=" + nextUrl +">");
  }
  else {
    // the second request
    Cookie[] cookies = request.getCookies();
    boolean cookieFound = false;
    if (cookies!=null) {
      int length = cookies.length;
      for (int i=0; i<length; i++) {
        Cookie cookie = cookies[i];
        if 
(cookie.getName().equals("cookieSetting") &&
          cookie.getValue().equals("on")) {
          cookieFound = true;
          break;
        }
      }
    }
    if (cookieFound) {
      out.println("Cookie is on.");
    }
    else {
      out.println("Cookie is off.");
    }
  }
%>

About the Author
Budi Kurniawan is an IT consultant specializing in Internet and object-oriented programming and has taught both Java and Microsoft technologies. He is the author of the best-selling Java for the Web with Servlets, JSP, and EJB: A Developer's Guide to Scalable Solutions (New Riders) and the developer of the most popular Java Upload Bean from BrainySoftware.com, which is licensed and used in projects in major corporations. Contact Budi at .



Back to top

Printer-Friendly Version












Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home