【发布时间】:2012-11-22 12:19:02
【问题描述】:
环境
- Eclipse Juno 服务版本 1
- GWT 2.5
- 带有 GWT 开发者插件的谷歌浏览器
- 使用“运行应用程序”作为 Google Web 应用程序在 Jetty 服务器上运行 GWT
我正在尝试使用以下代码使用 gwt 设置 2 个 cookie:
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 的 long。
更新
使用
if(Cookies.getCookie("opsession") == null) {
System.out.println("opsession: cookie not found at all.");
}
我已经确认 cookie 根本没有放置,并且没有“null”字符串值。
我也变了
ClientUser.SESSION_EXPIRY_TIME_IN_MINUTES 变成了长号。
更新
Fiddler 确认 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 数据。
【问题讨论】:
-
不应该是
ClientUser.SESSION_EXPIRY_TIME_IN_MINUTES * 60000将其转换为毫秒。 -
您有我无比真诚的感谢!我这样使用它是因为在服务器上,我使用
HttpSession.setMaxInactiveInterval()行,这需要几秒钟的时间!请将此作为答案发布,因为我非常想检查它! :)
标签: java gwt session-cookies