Session handling in GWT RequestFactory
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_RequestFacto开发者_如何学GoryServlet.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?
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.
精彩评论