【问题标题】:isNew returns false even when I have created a new session.Why is that ?即使我创建了一个新会话,isNew 也会返回 false。为什么会这样?
【发布时间】:2014-05-31 13:33:37
【问题描述】:

在下面的 sn-p 中,uSession.isNewalways 返回 false。这是为什么 ?我创建了一个新会话,但相应的方法返回false

HttpSession uSession = request.getSession();
    if(uSession.isNew()) {System.out.println("Inside if statement---------");
        uSession.setAttribute("username", username);
        uSession.setMaxInactiveInterval(3600);
        RequestDispatcher dispatcher = request.getRequestDispatcher("User.jsp");
        dispatcher.forward(request, response);
    }

我什至尝试从浏览器中删除所有 localhost cookie。

【问题讨论】:

  • 尝试使用HttpSession#invalidate
  • 因为 HttpSession 在 request.getSession() 调用之前就存在,所以 isNew 返回 false。需要检查在 HttpSession uSession = request.getSession(); 之前创建 httpSession 的位置/时间;
  • 另外,您没有在此代码中创建新会话。
  • getSession(false) 如果一个新的 Session 尚不存在,则不会返回。并检查会话是否为空
  • @LuiggiMendoza 新会话?你的意思是传递一个布尔值?

标签: java session jakarta-ee httpsession


【解决方案1】:

Jsp 自动创建会话。我不知道它为什么会创建。但确实如此。所以你的jsp会自动创建一个会话,这就是isNew()一直返回false的原因。 解决方案是在 JSP Page Directive 中将 session 设置为 false。 IE。, 使用<%@ page session="false"%

【讨论】:

    猜你喜欢
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    • 2021-06-21
    • 2015-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多