request.getSession()和request.getSession(true)意思相同:获取session,如果session不存在,就新建一个
reqeust.getSession(false)获取session,如果session不存在,则返回null
如果 项目中无法确定回话一定存在,最好用request.session(false);
getSession(boolean create)意思是返回当前reqeust中的HttpSession ,如果当前reqeust中的HttpSession 为null,当create为true,就创建一个新的Session,否则返回null;
简而言之:
HttpServletRequest.getSession(ture)等同于 HttpServletRequest.getSession()
HttpServletRequest.getSession(false)等同于 如果当前Session没有就为null;
getSession是返回当前用户的会话对象
request.getSession() 与 request.getSession(true) 在效果上没有区别。
request.getSession(true) ,“当前用户的会话对象”为空(第一次访问时)则创建一个新的会话对象返回;
request.getSession(false) ,“当前用户的会话对象”为空,则返回null(即不自动创建会话对象)
request.getSession(boolean create)
如果有与当前的request相关联的HttpSession,那么返回与当前request关联的HttpSession,
如果还没有,那么:
如果 create == true 那么返回一个新建的HttpSession;
如果 create == false,那么返回 null。
HttpSession session = request.setSession(); //建立新的session
session .setAttribute(“A”,”a”);//在session中存值
HttpSession session = request.getSession(true/false);//获取session
session getAttribute(“A”);//获取session中对象
在前台获取session中对象,${A!};