通常jsp编程中都是使用
HttpServletRequest.getSession()
但却忽略了一个问题,如果此用户session不存在,则会创建一个新的session。
可以看到官方解释:
/**
*Returns the current HttpSession associated with this request or, if if there
*is no current session and create is true, returns a new session.
*If create is false and the request has no valid HttpSession, this method
*returns null.
*To make sure the session is properly maintained, you must call this method
*before the response is committed. If the container is using cookies to maintain
*session integrity and is asked to create a new session when the response is
*committed, an IllegalStateException is thrown.
*Parameters: true - to create a new session for this request if necessary; false
*to return null if there's no current session
*Returns: the HttpSession associated with this request or null if create is
*false and the request has no valid session
*/
public HttpSession getSession(boolean create)
而
HttpServletRequest.getSession() == HttpServletRequest.getSession(ture)
HttpServletRequest.getSession(false) == 如果不存在则返回null
正确处理方式应该是:
使用HttpServletRequest.getSession(false)处理session。
HttpSession _session = HttpServletRequest.getSession(false);
if (_session != null) {
_session.getAttribute("xxx");
}
分享到:
相关推荐
request.getSession().doc
主要介绍了java 中 request.getSession(true/false/null)的区别的相关资料,需要的朋友可以参考下
在网上经常看到有人对request.getSession(false)提出疑问,我第一次也很迷惑,看了一下J2EE1.3 API,看一下官网是怎么解释的。
description The server encountered an internal error () that prevented it from fulfilling this request. exception javax.servlet.ServletException: Servlet execution threw an exception root cause ...
Map, Book> books = (Map, Book>)request.getSession().getServletContext().getAttribute("books"); Book book = books.get(bookid); System.out.println(book); //得到数量 int bookNum = Integer....
request.getSession (). GetAttributes() 答案: A 2. JSP页面需要创建仅在本页面使用的JavaBean的示例,为了完成此功能必须使用jsp:useBean的哪两个属性进行设置?(选择两个选项) A. id B. type C. name D. ...
HttpSession session = request.getSession(); // session.setAttribute("username",username); session.setAttribute("user",user); //response.sendRedirect("/myservlet2/admin/success.jsp"); //response....
在后台通过Request取值为null,是因为只设置了id属性,而取值候用的是name属性,问题就出现在这里
String planeImage = request.getSession().getServletContext().getRealPath("/image").replace("\\", "/")+"/"+"symark.png"; //获取目标图片的路径String targetPic = request.getSession().getServletContext()....
HttpSession session= request.getSession(); // 设置session的值 session.setAttribute("userList", list); //跳转到显示的页面,格式(得到当前页面的+要跳转的页面) response.sendRedirect(request....
UserAccessToken token = (UserAccessToken) request.getSession().getAttribute("UserAccessToken"); if(null==token){ token = util.getAccessToken3(Constants.APPID, Constants.SECRET,code); ...
.getSession().getServletContext()); MultipartHttpServletRequest multipartRequest = resolver .resolveMultipart(request); MultipartFile file = multipartRequest.getFile("fileList"); 这个file就能...
js:(jsp页面引入md5.js文件) var mobile=$("input[name='mobile']").val(); var hash1=hex_md5(mobile); $("input[name='hidmobile']").val... request.getSession().setAttribute("actList", actList); } }
HttpSession session = request.getSession(); session.setAttribute("userName", name); session.setAttribute("pwd", pwd); session.setAttribute("msgList", msgList); response.sendRedirect("jspPages/...
1、在JSP页面中用标记应用验证码。 ...HttpSession session = request.getSession(); String rancode = (String)session.getAttribute("random"); if(code.equals(rancode)){//判断用户输入的对否
HttpSession session = request.getSession(false); String cusername=(String) session.getAttribute("cusername"); ContentInfobiz contentInfobiz=new ContentInfobiz(); int c=contentInfobiz.addcont(c...
HttpSession session = request.getSession(); //是否登录 //开放注册页面 if(null==session.getAttribute("merchantInfo") &&request.getRequestURL().indexOf("regist/merchant/acount.jsp")==-1){ ...
List<Article> alist1 = (List)request.getSession().getAttribute("alist1"); Iterator<Article> in1 = alist1.iterator(); List<Article> alist2 = (List)request.getSession().getAttribute("alist2"); ...
if (httpssessionmap.containsKey(userid)&&httpssessionmap.get(userid).equals(event.getSession())) { //userIds.remove(userid); httpssessionmap.remove(userid); if(u!=null && userid....