W3C home > Mailing lists > Public > www-jigsaw@w3.org > November to December 1998

Setting cookies from Servlet.

From: Rajesh Joshi <rjoshi@arsin.com>
Date: Thu, 19 Nov 1998 15:10:45 -0500 (EST)
Message-ID: <01BE13B5.31D985A0@dynamic47.pm04.san-jose.best.com>
To: "'www-jigsaw@w3.org'" <www-jigsaw@w3.org>
Hi,

I am trying to set cookie in response header & then read the same information in another
servlet. Sample code for setting is :

Cookie c = new Cookie("Test", "yes");
c.setDomain("."+req.getServerName());
System.out.println("Server Name: " + req.getServerName());
c.setMaxAge(100000);
res.addCookie(c);

When I try to read the same, in the following piece of code:

   Cookie cookies[] = null;
    boolean rv = false;
    String strValue = new String();

    System.out.println("readCookies");
    try{
        try {
            
             cookies = req.getCookies();
                 System.out.println(cookies);
        }
        catch (NullPointerException e) 
        {
            System.out.println("Cookie Servlet::ReadCookie failed");
            return rv;
        }

        if(cookies != null){
            for(int i=0; i < cookies.length; i++){
                Cookie thisCookie = cookies[i];
                System.out.println("Cookie Server : " +thisCookie.getDomain());
                String strCookieInfo = thisCookie.getName();
                System.out.println(strCookieInfo);
                
                if (strCookieInfo.equals("Test")) {
                    strValue = thisCookie.getValue();
                    System.out.println("Cookie UID : " + strValue);
                    rv = true;
                }

The get the "Null pointer exception" with message "Cookie Servlet::ReadCookie failed."
The cookie.txt shows no entry for the cookie. 

Regards,

Rajesh Joshi
Arsin Corporation.
Received on Tuesday, 24 November 1998 03:23:52 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Monday, 9 April 2012 12:13:28 GMT