【问题标题】:A JSP page creates a JSESSIONID cookie where not appropriateJSP 页面在不合适的地方创建 JSESSIONID cookie
【发布时间】:2019-06-23 16:11:52
【问题描述】:

当我访问我的页面.../index.jsp 而没有HttpSessions 时,index.jsp 仍然会创建JSESSIONID-cookie。更糟糕的是,在负责注销人员的 servlet 中,session.invalidate() 似乎无法解决问题。

index.jsp 看起来像这样:

<%@page import="javax.servlet.http.Cookie"%>
<%@page contentType="text/html" pageEncoding="utf-8"%>
<%@page session="true"%>

<%!
    void removeJSessionIdCookie(HttpServletResponse response) {
        Cookie cookie = new Cookie("JESSIONID", "");
        cookie.setValue(null);
        cookie.setMaxAge(0);
        cookie.setPath("/");
        response.addCookie(cookie);
    }
%>


<%
    if (session != null) {
        out.print("Session not null.");
        if (session.getAttribute(Config.CURRENT_USER_ATTRIBUTE) != null) {
            out.print("have user"); 
            request.getRequestDispatcher("app.jsp").forward(request, response);
            return;
        } else {
            out.println("no user here");
            session.invalidate();
            removeJSessionIdCookie(response);
        }
    }
%>
<html>...</html>

【问题讨论】:

    标签: java jsp session


    【解决方案1】:

    如果您的&lt;%@page%&gt; 指令中有session="true",那么如果调用客户端没有带会话cookie,即还没有会话,JSP 框架代码总是会创建一个新会话。

    您需要将session="false" 放入页面指令中;这会使框架停止为您创建会话。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-11
      • 2018-05-25
      • 2016-01-04
      • 2013-06-28
      • 2017-03-09
      • 2011-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多