code: public static void widenDomain (HttpServletRequest req, Cookie cookie)
      {
          String server = req.getServerName();
          int didx = server.indexOf(".");
          // if no period was found (e.g. localhost) don't set the domain
          if (didx == -1) {
              return;
          }
          // if two or more periods are found (e.g. www.domain.com) strip up to the first one
          if (server.indexOf(".", didx+1) != -1) {
              cookie.setDomain(server.substring(didx));
          } else {
              // ...otherwise prepend a "." because we're seeing something like "domain.com"
              cookie.setDomain("." + server);
          }
      }
docs: Sets the domain of the specified cookie to the server name associated with the supplied
  request minus the hostname (ie. <code>www.samskivert.com</code> becomes
  <code>.samskivert.com</code>).