I solved it by finding out that while creating a cookie, you also have to set the path for it. So in the server side,
Cookie nameCookie = new Cookie("name","adam");
nameCookie.setPath("/");
response.addCookie(nameCookie);
Now the following client side code returns the proper value as adam
String name = Cookies.getCookie("name");