// There cannot be a session if no context has been assigned yet // 1. 检验 StandardContext if (context == null) { return (null); }
// Return the current session if it exists and is valid // 2. 校验 Session 的有效性 if ((session != null) && !session.isValid()) { session = null; } if (session != null) { return (session); }
// Return the requested session if it exists and is valid Managermanager=null; if (context != null) { //拿到StandardContext 中对应的StandardManager,Context与 Manager 是一对一的关系 manager = context.getManager(); } if (manager == null) { return (null); // Sessions are not supported } if (requestedSessionId != null) { try { // 3. 通过 managerBase.sessions 获取 Session // 4. 通过客户端的 sessionId 从 managerBase.sessions 来获取 Session 对象 session = manager.findSession(requestedSessionId); } catch (IOException e) { session = null; } // 5. 判断 session 是否有效 if ((session != null) && !session.isValid()) { session = null; } if (session != null) { // 6. session access +1 session.access(); return (session); } }
// Create a new session if requested and the response is not committed // 7. 根据标识是否创建 StandardSession ( false 直接返回) if (!create) { return (null); } // 当前的 Context 是否支持通过 cookie 的方式来追踪 Session if ((context != null) && (response != null) && context.getServletContext().getEffectiveSessionTrackingModes().contains(SessionTrackingMode.COOKIE) && response.getResponse().isCommitted()) { thrownewIllegalStateException (sm.getString("coyoteRequest.sessionCreateCommitted")); }
// Attempt to reuse session id if one was submitted in a cookie // Do not reuse the session id if it is from a URL, to prevent possible // phishing attacks // Use the SSL session ID if one is present. // 8. 到这里其实是没有找到 session, 直接创建 Session 出来 if (("/".equals(context.getSessionCookiePath()) && isRequestedSessionIdFromCookie()) || requestedSessionSSL ) { session = manager.createSession(getRequestedSessionId()); // 9. 从客户端读取 sessionID, 并且根据这个 sessionId 创建 Session } else { session = manager.createSession(null); }
// Creating a new session cookie based on that session if ((session != null) && (getContext() != null)&& getContext().getServletContext().getEffectiveSessionTrackingModes().contains(SessionTrackingMode.COOKIE)) { // 10. 根据 sessionId 来创建一个 Cookie Cookiecookie= ApplicationSessionCookieConfig.createSessionCookie(context, session.getIdInternal(), isSecure()); // 11. 最后在响应体中写入 cookie response.addSessionCookieInternal(cookie); }
// There cannot be a session if no context has been assigned yet // 1. 检验 StandardContext if (context == null) { return (null); }
// Return the current session if it exists and is valid // 2. 校验 Session 的有效性 if ((session != null) && !session.isValid()) { session = null; } if (session != null) { return (session); }
// Return the requested session if it exists and is valid Managermanager=null; if (context != null) { //拿到StandardContext 中对应的StandardManager,Context与 Manager 是一对一的关系 manager = context.getManager(); } if (manager == null) { return (null); // Sessions are not supported } if (requestedSessionId != null) { try { // 3. 通过 managerBase.sessions 获取 Session // 4. 通过客户端的 sessionId 从 managerBase.sessions 来获取 Session 对象 session = manager.findSession(requestedSessionId); } catch (IOException e) { session = null; } // 5. 判断 session 是否有效 if ((session != null) && !session.isValid()) { session = null; } if (session != null) { // 6. session access +1 session.access(); return (session); } }
// Create a new session if requested and the response is not committed // 7. 根据标识是否创建 StandardSession ( false 直接返回) if (!create) { return (null); } // 当前的 Context 是否支持通过 cookie 的方式来追踪 Session if ((context != null) && (response != null) && context.getServletContext().getEffectiveSessionTrackingModes().contains(SessionTrackingMode.COOKIE) && response.getResponse().isCommitted()) { thrownewIllegalStateException (sm.getString("coyoteRequest.sessionCreateCommitted")); }
// Attempt to reuse session id if one was submitted in a cookie // Do not reuse the session id if it is from a URL, to prevent possible // phishing attacks // Use the SSL session ID if one is present. // 8. 到这里其实是没有找到 session, 直接创建 Session 出来 if (("/".equals(context.getSessionCookiePath()) && isRequestedSessionIdFromCookie()) || requestedSessionSSL ) { session = manager.createSession(getRequestedSessionId()); // 9. 从客户端读取 sessionID, 并且根据这个 sessionId 创建 Session } else { session = manager.createSession(null); }
// Creating a new session cookie based on that session if ((session != null) && (getContext() != null)&& getContext().getServletContext().getEffectiveSessionTrackingModes().contains(SessionTrackingMode.COOKIE)) { // 10. 根据 sessionId 来创建一个 Cookie Cookiecookie= ApplicationSessionCookieConfig.createSessionCookie(context, session.getIdInternal(), isSecure()); // 11. 最后在响应体中写入 cookie response.addSessionCookieInternal(cookie); }