会话对象返回 null

发布于 2024-08-24 19:36:48 字数 96 浏览 3 评论 0原文

我为我的项目“学生信息系统”创建了一个登录页面,其中包含文本字段用户名、用户 ID 和密码。我尝试将用户名和用户 ID 的值存储在会话对象中并在另一个页面中使用它,但它返回空值。

i have created a login page for my project "student information system" which contain text fields username,userid and password.i tried to store the value of username and userid in session object and use it in another page but it is returning null value.

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

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

发布评论

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

评论(1

心如荒岛 2024-08-31 19:36:48

有几个原因:

  1. 您以错误的方式将其放入会话中(应该通过 session.setAttribute(key, value) 完成)。
  2. 另一个页面位于不同的域/上下文中(因此不使用相同的会话)。
  3. 有些东西使会话无效(例如session.invalidate())。
  4. 客户端不支持cookie,并且您还没有添加URL重写支持。
  5. 您在会话中以错误的方式访问它(应通过 session.getAttribute(key)${key} 完成)。

下面的 login.jsp 示例..

<form action="login" method="post">
    Username: <input type="text" name="username"><br>
    Password: <input type="password" name="password"><br>
    <input type="submit" value="login">
    <span class="error">${error}</span>
</form>
<p>Debug info: your current session ID is ${pageContext.session.id}</p>

..有一个映射到 /login 的 servlet,它在 doPost() 中基本上具有以下内容..

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    User user = userDAO.find(username, password);
    if (user != null) {
        request.getSession().setAttribute("user", user);
        response.sendRedirect(response.encodeRedirectURL("home.jsp"));
    } else {
        request.setAttribute("error", "Unknown login, try again");
        request.getRequestDispatcher("login.jsp").forward(request, response);
    }
}

. .和一个看起来像这样的 home.jsp ..

<p>Welcome, ${user.name}!</p>
<p>Debug info: your current session ID is ${pageContext.session.id}</p>

应该可以工作。

确保两个页面中的会话 ID相同。出于调试目的,您可以通过 ${pageContext.session.id} 查看它,如上面的 JSP 代码片段所示。如果相同并且 User 对象仍然不存在,则问题出在其他地方。

Several causes:

  1. You've put it in the session the wrong way (should be done by session.setAttribute(key, value)).
  2. The other page is in a different domain/context (and thus doesn't use the same session).
  3. Something has invalidated the session (e.g. session.invalidate()).
  4. The client doesn't support cookies and you haven't added URL rewriting support.
  5. You're accessing it in the session the wrong way (should be done by session.getAttribute(key) or ${key}).

The following login.jsp example..

<form action="login" method="post">
    Username: <input type="text" name="username"><br>
    Password: <input type="password" name="password"><br>
    <input type="submit" value="login">
    <span class="error">${error}</span>
</form>
<p>Debug info: your current session ID is ${pageContext.session.id}</p>

..with a servlet mapped on /login which has basically the following in doPost()..

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    User user = userDAO.find(username, password);
    if (user != null) {
        request.getSession().setAttribute("user", user);
        response.sendRedirect(response.encodeRedirectURL("home.jsp"));
    } else {
        request.setAttribute("error", "Unknown login, try again");
        request.getRequestDispatcher("login.jsp").forward(request, response);
    }
}

..and a home.jsp which look like this..

<p>Welcome, ${user.name}!</p>
<p>Debug info: your current session ID is ${pageContext.session.id}</p>

..should just work.

Ensure that the session ID is the same in both pages. For debugging purposes you can view it by ${pageContext.session.id} as shown in the above JSP code snippets. If it is the same and the User object is still not there, then the problem lies somewhere else.

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