GWT RequestFactory 中的会话处理
有人可以向我展示一个有关使用 GWT RequestFactory 进行会话处理的简单示例吗? 也许这很简单,但我无法弄清楚。
我在某处读到我应该使用 ThreadLocal。好吧,我做到了。这是我的代码:
public class EC_RequestFactoryServlet extends RequestFactoryServlet {
private static final ThreadLocal < HttpServletRequest > uniqueReq =
new ThreadLocal < HttpServletRequest > () {
@Override protected HttpServletRequest initialValue() {
return null;
}
};
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
{
uniqueReq.set(req);
super.doPost(req, res);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
uniqueReq.set(req);
super.doGet(req, res);
}}
这保存了会话:
public class Authentication {
public static void SetLoggedInUserToSession(User user) {
EC_RequestFactoryServlet.getThreadLocalRequest()
.getSession().setAttribute("LOGGED_IN_USER", user);
}
public static User GetLoggedInUserFromSession(){
return (User)EC_RequestFactoryServlet.getThreadLocalRequest()
.getSession().getAttribute("LOGGED_IN_USER");
}}
登录后,没问题,但在另一个请求后,会话为空。
那么我错过了什么?或者这是更好的方法?
Can somebody show me a simple example about session handling with GWT RequestFactory.
Maybe it is very simple, but I can't figure it out.
I read somewhere that I should use ThreadLocal. Well I did. Here is my code:
public class EC_RequestFactoryServlet extends RequestFactoryServlet {
private static final ThreadLocal < HttpServletRequest > uniqueReq =
new ThreadLocal < HttpServletRequest > () {
@Override protected HttpServletRequest initialValue() {
return null;
}
};
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
{
uniqueReq.set(req);
super.doPost(req, res);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
uniqueReq.set(req);
super.doGet(req, res);
}}
And this saves the session:
public class Authentication {
public static void SetLoggedInUserToSession(User user) {
EC_RequestFactoryServlet.getThreadLocalRequest()
.getSession().setAttribute("LOGGED_IN_USER", user);
}
public static User GetLoggedInUserFromSession(){
return (User)EC_RequestFactoryServlet.getThreadLocalRequest()
.getSession().getAttribute("LOGGED_IN_USER");
}}
After the login, it is okay, but after another request, the session is empty.
So what am I missing? Or is it a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
或者您可以使用
RequestFactoryServlet.getThreadLocalRequest()
。Or you could just use
RequestFactoryServlet.getThreadLocalRequest()
.哦,现在可以用了。我在程序的其他地方犯了一个愚蠢的错误。上面的代码工作正常。
Oh, it's working now. I made a silly mistake somewhere else in the program. The code above it's working fine.