JSP Session

session 隐式对象、会话跟踪、超时与登出。

Beginner
Key Pointssession 是每个用户一份的服务端存储,靠 JSESSIONID 关联;关掉浏览器 Cookie 后它才真正失效。

基本操作

JSP
<%
    // 存
    session.setAttribute("uid", 42);
    session.setAttribute("cart", new java.util.ArrayList<String>());

    // 取
    Object uid = session.getAttribute("uid");

    // 删单个
    session.removeAttribute("uid");

    // 会话信息
    String id = session.getId();
    long created = session.getCreationTime();
    long last = session.getLastAccessedTime();
    int timeout = session.getMaxInactiveInterval();   // 秒
%>

EL 读取

JSP
<p>用户 ID:${sessionScope.uid}</p>
<p>购物车:${sessionScope.cart.size()} 件</p>
<p>会话 ID:${pageContext.session.id}</p>

${uid} searches page → request → session → application. It works but is vague; ${sessionScope.uid} states the source at a glance.

登录与登出

JSP
<%-- login.jsp 提交到 doLogin.jsp --%>
<%
    String user = request.getParameter("user");
    String pass = request.getParameter("pass");
    if (userService.check(user, pass)) {
        session.setAttribute("uid", userService.idOf(user));
        response.sendRedirect("home.jsp");
        return;
    }
%>
<p>用户名或密码错误</p>
JSP
<%-- logout.jsp --%>
<%
    session.invalidate();                    // 销毁整个会话
    response.sendRedirect("login.jsp");
    return;
%>

Logout must call invalidate(). Removing just uid leaves other data and keeps the session id valid, so a stolen id still works.

超时配置

XML
<session-config>
  <session-timeout>30</session-timeout>     <!-- 分钟 -->
</session-config>
JSP
<%
    session.setMaxInactiveInterval(15 * 60);   // 单独给这个会话设 15 分钟
%>

URL 重写

JSP
<%-- 浏览器禁用 Cookie 时,把 session id 编进 URL --%>
<a href="<%= response.encodeURL("cart.jsp") %>">购物车</a>
<a href="<%= response.encodeRedirectURL("checkout.jsp") %>">结算</a>

encodeURL appends ;jsessionid=... only when needed and returns the URL unchanged when cookies work. The cost is ugly URLs and leaked ids via logs or Referer, so use it only for compatibility.

Sessions live in container memory: a restart loses them and clustered servers do not share them. Surviving restarts or scaling out needs persistence or a central store such as Redis — not a one-line config change.

Friends & Partners