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

前端 未结 8 724
礼貌的吻别
礼貌的吻别 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 05:07

    First line will return the "session id" on server. The second line will return session object. So what will be printed on system.out would be request.getSession(false).toString();

    The default implementation of toString returns the "object id". Object id in terms of session is not the same as session id. Session could be serialized and replicated across the cluster so on each node of the cluster on each JVM it may have it's own object id (but should have same session id).

    Calling get session with boolean is explained here: http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getSession(boolean)

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

    HttpSession session = request.getSession(); Inside the service method we ask for the session and every thing gets automatically, like the creation of the HttpSession object. There is no need to generate the unique session id. There is no need to make a new Cookie object. Everything happens automatically behind the scenes.

    As soon as call the method getSession() of the request object a new object of the session gets created by the container and a unique session id generated to maintain the session. This session id is transmitted back to the response object so that whenever the client makes any request then it should also attach the session id with the requsest object so that the container can identify the session.

    request.getSession(false) : This method will check whether Session already existed for the request or not. If it existed then it will return the already existed Session. If Session is not already existed for this request then this method will return NULL, that means this method says that the request does not have a Session previously.

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