java会话跟踪

发布于 2024-12-11 14:17:38 字数 551 浏览 3 评论 0原文

可能的重复:
Java页面重定向

我想在第一次调用某个页面时转到该页面并在会话仍处于活动状态时始终转到另一个页面。我已经有了一个登录机制。我做了另一个主题,并建议在会话层设置一个标志。我不知道如何实现这个。

基本上,如果当他们按下按钮转到某个页面时,它会这样调用:

public int show(Action action) throws Exception {
    HttpServletRequest request = action.getRequest();
    action.setJspURI("objects/objects_guidlines.jsp");
    return FORWARD;
}

如果他们是第一次来这里,那就没问题。我如何实施检查以查看这是否是第一次,然后发送到另一个页面?

Possible Duplicate:
Java page re-direct

I want to go to a certain page the first time it calls that page and go to another page all times after that while the session is is still active. I already have a login mechanism in place. I made another topic and was suggested to set a flag on the session layer. I'm not sure how to implement this.

Basically if when they press a button to go to a page it calls this:

public int show(Action action) throws Exception {
    HttpServletRequest request = action.getRequest();
    action.setJspURI("objects/objects_guidlines.jsp");
    return FORWARD;
}

This is fine if they are coming here for the first time. How can I implement a check to see if this the first time or not and subsequently send to another page?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

说谎友 2024-12-18 14:17:38

HTTP 是一种无状态协议,因此如果您想在请求之间维护状态(例如用户之前是否来过这里),您需要使用 cookie 或会话之类的东西。

如果要使用会话,可以从 HttpServletRequest 获取它并执行以下操作:

public int show(Action action) throws Exception{
    HttpServletRequest request = action.getRequest();
    if (request.getSession().getAttribute("has_visited_previously")!=true)
    {
        //User has not been here before so record that they are visiting.
        request.getSession().setAttribute("has_visited_previously", true);
        action.setJspURI("objects/objects_guidlines.jsp");
        return FORWARD;
    }
    else
    {
        //User has been here before.
        action.setJspURI("objects/user_has_visited_before.jsp");
        return FORWARD;
    }
}

HTTP is a stateless protocol so if you want to maintain state between requests (such as whether the user has been here before), you'll need to use something like cookies or sessions.

If you want to use the session, you can get it from the HttpServletRequest and do the following:

public int show(Action action) throws Exception{
    HttpServletRequest request = action.getRequest();
    if (request.getSession().getAttribute("has_visited_previously")!=true)
    {
        //User has not been here before so record that they are visiting.
        request.getSession().setAttribute("has_visited_previously", true);
        action.setJspURI("objects/objects_guidlines.jsp");
        return FORWARD;
    }
    else
    {
        //User has been here before.
        action.setJspURI("objects/user_has_visited_before.jsp");
        return FORWARD;
    }
}
鸠书 2024-12-18 14:17:38

您需要一个静态变量或单例类来检查这一点。

在 servlet 中,您可以声明一个静态变量来强制执行此检查,如果您无权访问该变量,则可以使用单例。

答案还取决于您的架构。

You would need a static variable or a singleton class to check this.

In servlets you can declare a static variable to enforce this check, if you do not have access to that then you can use a singleton.

Answer also depends upon your architecture.

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