By default, cookies are accessible to every HTTP request for the current directory, and any subdirectories. On most Web servers and servlet engines, servlets are located in a special directory. For example, when using a servlet runner (which ships with the Java Servlet Development Kit), servlets must be invoked under the /servlet/ directory:
If you want your cookies to be accessible elsewhere, you must specify the root path of your Webserver, using the avax.servlet.Cookie.setPath( String ) method.
// Create a cookie for everyone to shareCookie myCookie = new Cookie ("counter", "1");// Set path for cookiemyCookie.setPath( "/" );
Once you’ve set the path, any script (for example, stored in /cgi-bin/) can access the cookies stored by your servlets.