2012-11-22 53 views
2

环境GWT Cookie不能设置或获取

  • 的Eclipse朱诺服务发布1
  • GWT 2.5
  • 谷歌浏览器与GWT开发插件
  • 使用“运行Jetty服务器上运行GWT应用程序“作为谷歌网络应用程序

我想设置2饼干usin g gwt使用以下代码:

if(result.getStatus() == ServerResponse.SUCCESS) { 
    System.out.println("I will now set cookies: " + result.getMessage() + " and " + Integer.toString(result.getValue().getId())); 
    Cookies.setCookie("opsession", result.getMessage(), new Date(System.currentTimeMillis() + ClientUser.SESSION_EXPIRY_TIME_IN_MINUTES)); 
    Cookies.setCookie("opuser", Integer.toString(result.getValue().getId()), new Date(System.currentTimeMillis() + ClientUser.SESSION_EXPIRY_TIME_IN_MINUTES)); 
    System.out.println("Cookie set: session: " + Cookies.getCookie("opsession")); 
    main.checkLoginAndRedirect(); 
    System.out.println("Redirected."); 
} else if(result.getStatus() == ServerResponse.FAILURE){ 
    new MessageBox(result.getShortTitle(), result.getMessage()).show(); 
} 

它似乎不工作。 println的公司在那里进行调试,这里是输出:

I will now set cookies: 1er1qmaly9ker and 1 
Cookie set: session: null 
nullnull 
Redirected. 

ClientUser.SESSION_EXPIRY_TIME_IN_MINUTES是(为一个int),长返回20

更新

使用

if(Cookies.getCookie("opsession") == null) { 
    System.out.println("opsession: cookie not found at all."); 
} 

我已经确认cookie根本没有放置,并且没有'null'字符串值。

我也改变了 ClientUser.SESSION_EXPIRY_TIME_IN_MINUTES成长。

更新

小提琴手确认cookie数据已发出:

Response sent 30 bytes of Cookie data: Set-Cookie: JSESSIONID=4kr11hs48gvq;Path=/

但是,只有当我使用setCookie方法的更长的版本:

Cookies.setCookie("opuser", Integer.toString(result.getValue().getId()), new Date(System.currentTimeMillis() + ClientUser.SESSION_EXPIRY_TIME_IN_MINUTES), null, "/", false); 

如果我使用String, String, long变体,fiddler通知没有cookie数据。

+1

不应该是'ClientUser.SESSION_EXPIRY_TIME_IN_MINUTES * 60000'将其转换为毫秒。 –

+0

你有我非常诚挚的谢意!我以这种方式使用它,因为在服务器上,我使用'HttpSession.setMaxInactiveInterval()'行,这需要几秒钟的时间! 请张贴这个答案,因为我非常想检查它! :) –

回答

2

应该

new Date(System.currentTimeMillis() 
+ (ClientUser.SESSION_EXPIRY_TIME_IN_MINUTES * 60000))) 

的到期时间转换为毫秒。

+0

好:)。谢谢! –