session.getId()和request.getRequestedSessionId()不一样!!!

发布时间 2023-10-27 15:39:40作者: 逐梦北京

request.getSession().getId()将返回服务器的会话ID(如果会话不存在,request.getSession()将创建它)。
request.getRequestedSessionId()是从cookie中获取的~~ ,如果浏览器没有cookie,或者客户端没有指定cookie,它就是空的。
真的太不靠谱了~~
mmp

getRequestedSessionId:没有指定,则为null~~

    /**
     * Returns the session ID specified by the client. This may not be the same
     * as the ID of the current valid session for this request. If the client
     * did not specify a session ID, this method returns <code>null</code>.
     *
     * @return a <code>String</code> specifying the session ID, or
     *         <code>null</code> if the request did not specify a session ID
     * @see #isRequestedSessionIdValid
     */
    public String getRequestedSessionId();

getSession:没有Session则创建

    /**
     * Returns the current session associated with this request, or if the
     * request does not have a session, creates one.
     *
     * @return the <code>HttpSession</code> associated with this request
     * @see #getSession(boolean)
     */
    public HttpSession getSession();

https://stackoverflow.com/questions/38797066/request-getsession-getid-vs-request-getrequestedsessionid



作者:萧萧飞叶
链接:https://www.jianshu.com/p/d5efdb320712
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。