Cookie issue within Vaadin

Here is my code piece for cookie usage - I defined the following methods in my main app. In my another code (e.g. after user login submit) - I called saveCookie method like this: saveCookie(“my_userid”, userId, 36002430); // for keeping 1-month until expired

However it doesn’t work as it just returns null (the cookie is not found, not because of null http request handle) when calling getCookie method for giving the same cookie ID.

BTW, the browser (no matter IE or Chrome) I use enables cookie function, and I know I can use cookie when the browser accesses to some other web sites e.g. user login function, it does automatically load my login ID/pwd…

Did I get http request/response in a wrong way? Can you anyone suggest me how to solve this issue?

// below is code piece …

class MyMainApp extends Application implements HttpServletRequestListener
{

public void saveCookie(String key, String val, int age)
{
if(theResponse==null) return;
javax.servlet.http.Cookie cookie = new javax.servlet.http.Cookie(key, val);
cookie.setMaxAge(age);
theResponse.addCookie(cookie);
}

    public String getCookie(String key)
    {
    	if(theRequest==null) 
    	{
    		return "no_http_request_handle";
    		// return null;
    	}
    	System.out.println("Walk-through cookie...");
    	javax.servlet.http.Cookie[] cookies = theRequest.getCookies();
    	String value = null;
    	if (cookies != null) 
    	{
    		for (int i=0; i< cookies.length; i++) 
    		 { 
    			System.out.println("cookie name:" + cookies[i]

.getName() + “,value:”+ cookies[i]
.getValue());
if(cookies[i]
.getName().equals(key)) // to find the matching cookie object
{
value = cookies[i]
.getValue();
break;
}

    		 }
    		 return value;
    	} 
    	else 
    	{
    		System.err.println("Null cookie[] from theRequest.getCookies() !");
    		return null; 
    	}
    }
    
        // how my code to hold response and request handles as below ... 
        private HttpServletResponse theResponse = null;
    private HttpServletRequest theRequest = null;

    public void onRequestStart(HttpServletRequest request,	HttpServletResponse response)
    {
    		// Store the reference to the response object for using it in event listeners
    		theResponse = response;
    		theRequest = request;
    		
    }

Anyone would like to give some help on this issue?

Take a look at the
BrowserCookies add-on
. You could either use it directly or borrow the approach it uses.