Difference between request.getSession().getId() and request.getSession(false)?

前端 未结 8 723
礼貌的吻别
礼貌的吻别 2021-01-31 04:08

What do these calls actually mean in terms of session?

System.out.println(\"print1: \"+request.getSession().getId());
System.out.println(\"print2: \"+request.get         


        
相关标签:
8条回答
  • 2021-01-31 04:45
    request.getSession().getId()
    

    Returs the id of the session.

    Request.getsession(false) returns the already existing session object. It wont return anything i.e. Will return null , if session does not exist. Whereas with true parameter it will create a new session object and return it if no session exists

    0 讨论(0)
  • 2021-01-31 04:48
    request.getSession().getId();
    

    returns the unique identifier assigned to this session. And It has return type of String.

    request.getSession(false)
    

    returns the HttpSession object if it already exists else return null.

    0 讨论(0)
  • 2021-01-31 04:53
        request.getSession()
    

    This method will check for the existing session;if exist its return otherwise create a new session for the request.

        request.getSession().getId();
    

    This will return the unique identifier for that session.

        request.getSession(false);
    

    This method takes the boolean value.This method check whether there is an existing session present for that user(request);if exist it return that session otherwise it return null i.e it won't create new session.

    Just to add more information for session.

        request.getSession(true);
    

    This method checks for the existing current session for that user(request) and if the session exist it will return that session or otherwise it create new session for that user.

        request.getSession() works like request.getSession(true)
    

    Reference : http://docs.oracle.com/javaee/1.4/api/javax/servlet/http/HttpServletRequest.html#getSession%28boolean%29

    0 讨论(0)
  • 2021-01-31 04:55
    request.getSession().getId();
    

    this will return the id of the an existing session.

    Request.getsession(false) 
    

    it will return the session if it exists, or it will return null otherwise.

    and Request.getsession(false) means : give me the session if it exists, otherwise do not create a new instance (and thus return null).

    0 讨论(0)
  • 2021-01-31 05:05

    In short-

    request.getSession().getId() - returns a string containing the unique identifier assigned to this session. The identifier is assigned by the servlet container and is implementation dependent.

    request.getSession(false) - return session object or null if there's no current session.

    0 讨论(0)
  • 2021-01-31 05:05
    request.getSession().getId();
    

    Will return unique string id assigned to already started session. Generation of id is vendor specific like apache, jboss etc.

    request.getSession(false);
    

    It will return session object associated to particular request if session object is associated it will be returned or it will return null if its is not started by server.

    0 讨论(0)
提交回复
热议问题