request.getSession(false)とrequest.getSession(true)の違い



Difference Between Request



Difference between request.getSession(false) and request.getSession(true) The following code explains the difference between request.getSession(false) and request.getSession(true), not the j2ee source code. Personally think that the session saved in the server is saved with map: For example, define a map that holds all sessions. public GlobalClass { public static final Map map=new HashMap() } Get the map object in the HttpServletRequest implementation class (which includes the getSession() and getSession(boolean flag) methods) Map map=GlobalClass.map//Get the map object public HttpSession getSession(boolean flag) { / / In the case of request.getSession (true) / / If there is no requestedSessionId in the map (Note: requestedSessionId is an attribute in the HttpServletRequest implementation class, save the session Id number obtained from the client) key, //Create an HttpSession object and save it in the map if(flag==true) { // does not exist, create if(map.get(requestedSessionId)==null) { HttpSession session=new HttpSession implementation class () //The key in the map is the session.getSessionId() value. map.put(String.valueOf(session.getSessionId()),session) return session } //exist else { //requestedSessionId is a property in the HttpServletRequest implementation class HttpSession tempSession=(HttpSession)map.get(requestedSessionId) / / Get the session in the map, this time to determine whether the session has expired If (expired) { / / Set the value of the attribute attribute in the session to null tempSession.setAttribute(null) return tempSession } else { return tempSession } } } / / In the case of request.getSession (false) / / If the requestedSessionId key does not exist in the map, then return null, do not create else { // does not exist, returns null if(map.get(requestedSessionId)==null) { return null } //exist else { //requestedSessionId is a property in the HttpServletRequest implementation class HttpSession tempSession=(HttpSession)map.get(requestedSessionId) / / Get the session in the map, this time to determine whether the session has expired If (expired) { / / Set the value of the attribute attribute in the session to null tempSession.setAttribute(null) return tempSession } else { return tempSession } } } } //request.getSession() is the same as request.getSession(true) public HttpSession getSession() { return getSession(true) }

この記事はCSDNブログからのものです。ソースを示してください: http://blog.csdn.net/wzju64676266/archive/2009/12/13/4998968.aspx