读书人

request.getSession(false)跟request.

发布时间: 2012-09-15 19:09:28 作者: rapoo

request.getSession(false)和request.getSession(true)区别

request.getSession(false)和request.getSession(true)区别     以下代码解释一下request.getSession(false)和request.getSession(true)区别的思路,并不是j2ee源代码     个人觉得服务器中保存session是用map来保存的:   例如定义一个保存所有session的map     public GlobalClass   {       public static final Map map=new HashMap();          }       在HttpServletRequest实现类(其中包括getSession()和getSession(boolean flag)方法)中获得map对象   Map map=GlobalClass.map;//获得map对象     public HttpSession getSession(boolean flag)   {       //在request.getSession(true)情况下;       //如果map中不存在requestedSessionId(注:requestedSessionId是HttpServletRequest实现类中的一个属性,保存的是从客户端获取的session Id号)键的话,       //则创建一个HttpSession对象,并保存在map中       if(flag==true)       {            //不存在,创建            if(map.get(requestedSessionId)==null)            {                HttpSession session=new HttpSession的实现类();                  //map中的键是session.getSessionId()值                map.put(String.valueOf(session.getSessionId()),session);                return session;            }              //存在            else           {                //requestedSessionId是HttpServletRequest实现类中的一个属性                HttpSession tempSession=(HttpSession)map.get(requestedSessionId);                //获得map中的session,这个时候要判断session有没有过期;                if(过期)                {                   //将session中的attribute属性的值设为空                   tempSession.setAttribute(null);                   return tempSession;                }                else               {                   return tempSession;                 }            }         }              //在request.getSession(false)情况下;       //如果map中不存在requestedSessionId键的话,则返回null,不创建       else      {           //不存在,返回null            if(map.get(requestedSessionId)==null)            {                return null;            }            //存在            else           {                //requestedSessionId是HttpServletRequest实现类中的一个属性                HttpSession tempSession=(HttpSession)map.get(requestedSessionId);                //获得map中的session,这个时候要判断session有没有过期;                if(过期)                {                   //将session中的attribute属性的值设为空                   tempSession.setAttribute(null);                   return tempSession;                }                else               {                   return tempSession;                 }                            }       }           }     //request.getSession();跟request.getSession(true);是一样的   public HttpSession getSession()   {       return getSession(true);   }  

?

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wzju64676266/archive/2009/12/13/4998968.aspx

读书人网 >互联网

热点推荐