HttpServletRequest.getSession(ture) 等同于 HttpServletRequest.getSession(),若当前存在Session则返回该会话,否则新建一个Session;
HttpServletRequest.getSession(false) 等同于: 若当前存在Session则返回该会话,否则返回NULL,因此,一般情况下,尽量要明确使用参数为false的写法。
注意request.getSession() 等同于 request.getSession(true),除非要确认session一定存在或者sesson不存在时明确有创建session的需要,否则 尽量使用request.getSession(false)。在使用request.getSession()函数,通常在action中检查是否有某 个变量/标记存放在session中。这个场景中可能出现没有session存在的情况,正常的判断应该是这样:
HttpSession session = request.getSession(false);
if (session != null) {
String user_name = session.getAttribute("user_name");
}