GWT RequestFactory 中的会话处理

发布于 2024-12-07 11:33:47 字数 1277 浏览 0 评论 0原文

有人可以向我展示一个有关使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱已欠费 2024-12-14 11:33:47

哦,现在可以用了。我在程序的其他地方犯了一个愚蠢的错误。上面的代码工作正常。

Oh, it's working now. I made a silly mistake somewhere else in the program. The code above it's working fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文