开发者

Session handling in GWT RequestFactory

开发者 https://www.devze.com 2023-04-10 15:40 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号