request.getSession方法

request.getSession方法

request.getSession()

request.getSession(true)

request.getSession(false)

/javax/servlet/http/HttpServletRequest.java接口中三个方法

这三种方法调用的不同意义:

request.getSession()和request.getSession(true)的效果是一样的,如果当前存在会话session,返回该会话session;

如果不存在会话session,创建之并返回;request.getSession(false):如果当前存在会话session,返回会话session;

如果不存在,不会创建会话session,而会返回NULL;

下面是实现HttpServletRequest的/org/apache/catalina/connector/Request.java中的代码片段

看下面的代码:

@Override
public HttpSession getSession() {
    Session session = doGetSession(true);
    if (session == null) {
        return null;
    }

    return session.getSession();
}


@Override
public HttpSession getSession(boolean create) {
    Session session = doGetSession(create);
    if (session == null) {
        return null;
    }

    return session.getSession();
}
protected Session doGetSession(boolean create) {

    // There cannot be a session if no context has been assigned yet
    if (context == null) {
        return (null);
    }

    // Return the current session if it exists and is valid
    if ((session != null) && !session.isValid()) {
        session = null;
    }
    if (session != null) {
        return (session);
    }

    // Return the requested session if it exists and is valid
    Manager manager = null;
    if (context != null) {
        manager = context.getManager();
    }
    if (manager == null)
    {
        return (null);      // Sessions are not supported
    }
    if (requestedSessionId != null) {
        try {
            session = manager.findSession(requestedSessionId);
        } catch (IOException e) {
            session = null;
        }
        if ((session != null) && !session.isValid()) {
            session = null;
        }
        if (session != null) {
            session.access();
            return (session);
        }
    }

    // Create a new session if requested and the response is not committed
    if (!create) {
        return (null);
    }
    if ((context != null) && (response != null) &&
            context.getServletContext().getEffectiveSessionTrackingModes().
                    contains(SessionTrackingMode.COOKIE) &&
            response.getResponse().isCommitted()) {
        throw new IllegalStateException
                (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.
    if (("/".equals(context.getSessionCookiePath())
            && isRequestedSessionIdFromCookie()) || requestedSessionSSL ) {
        session = manager.createSession(getRequestedSessionId());
    } 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)) {
        Cookie cookie =
                ApplicationSessionCookieConfig.createSessionCookie(
                        context, session.getIdInternal(), isSecure());

        response.addSessionCookieInternal(cookie);
    }

    if (session == null) {
        return null;
    }

    session.access();
    return session;
}

通过代码可以看出,当create为false时,如果当前没有会话session,则返回null

if (!create) {
    return (null);
}

在上面有这么一段代码

session = manager.findSession(requestedSessionId);

接着来看:

org/apache/catalina/session/ManagerBase.java中

/**
 * Return the active Session, associated with this Manager, with the
 * specified session id (if any); otherwise return <code>null</code>.
 *
 * @param id The session id for the session to be returned
 *
 * @exception IllegalStateException if a new session cannot be
 *  instantiated for any reason
 * @exception IOException if an input/output error occurs while
 *  processing this request
 */
@Override
public Session findSession(String id) throws IOException {

    if (id == null)
        return (null);
    return sessions.get(id);

}

在接着看sessins属性是什么

/**
 * The set of currently active Sessions for this Manager, keyed by
 * session identifier.
 */
protected Map<String, Session> sessions = new ConcurrentHashMap<String, Session>();

这就应该是得到sessin的一系列方法调用,不知道对不对。

=======END=======

你可能感兴趣的:(request.getSession方法)