HttpServletRequest接口的getSession方法
2012-03-21 08:27
543 查看
关于HttpServletRequest接口的getSession方法,在页面提交请求到servlet时,用getSession(false)返回的却是一个session对象而不是null的问题。
getSession有两种方法,一个有参的、一个是无参的。
getSession()与getSession(true)一样,获取request对象关联的session对象,如果没有session,则返回一个新的session。
getSession(false)也是返回一个request对象关联的session对象,但如果没有session,则返回null。
Java EE 5 API中的定义:
HttpSession getSession(boolean create)
Returns the current
原因是Jsp页面会默认创建session对象,即<@page session="true">,默认是true,如果手动将session设为false,则jsp不会创建session对象,页面也就不可以直接使用session对象。这样提交到servlet,用方法getSession(false)返回的就是null。
原帖地址:http://blog.csdn.net/wing32/article/details/5618106
getSession有两种方法,一个有参的、一个是无参的。
getSession()与getSession(true)一样,获取request对象关联的session对象,如果没有session,则返回一个新的session。
getSession(false)也是返回一个request对象关联的session对象,但如果没有session,则返回null。
Java EE 5 API中的定义:
HttpSession getSession(boolean create)
Returns the current
HttpSessionassociated with this request or, if there is no current session and
createis true, returns a new session.If
createis
falseand the request has no valid
HttpSession, this method returns
null.在Jsp页面提交表单到servlet时,在servlet中调用getSession(false)时却返回了一个session,而不是null。
原因是Jsp页面会默认创建session对象,即<@page session="true">,默认是true,如果手动将session设为false,则jsp不会创建session对象,页面也就不可以直接使用session对象。这样提交到servlet,用方法getSession(false)返回的就是null。
原帖地址:http://blog.csdn.net/wing32/article/details/5618106
相关文章推荐
- HttpServletRequest接口的getSession方法
- HttpServletRequest接口的getSession方法
- HttpServletRequest和HttpServletResponse接口的方法
- java WEB学习笔记32:HttpSession 接口常用方法 及 HttpServletRequest接口中的Session方法 Demo
- HttpServletRequest接口中的getMethod()方法的实现
- HttpServletRequest接收参数的几种方法
- HttpServletRequest和HttpServletReponse部分方法汇总
- HttpServletResponse.sendRedirect()方法/RequestDispatcher.forward()方法
- springmvc中获得HttpServletRequest request方法(org.springframework.web.context.request.RequestContextList)
- 获取HttpServletRequest的方法
- 不同框架实现的WebService的服务端获取HttpServletRequest的方法
- HttpServletRequest、request常用方法、request常见应用、请求转发、RequestDispatcher
- 相对路径获取项目文件 及报错 No mapping found for HTTP request with URI XXX in DispatcherServlet with name ‘springmvc’解决方法
- HttpServletRequest和HttpSession中setAttribute/getAttribute方法究竟是用来干什么的?
- HttpServletRequest对象方法的用法
- java web给手机端写接口HttpServletRequest ,HttpServletResponse
- HttpServletResponse.sendRedirect方法和RequestDispatcher.forward方法区别
- HttpServletRequest的几个颇具争议的方法
- 警告: No mapping found for HTTP request with URI [/web002/list.do] in DispatcherServlet 的解决方法
- 请求重定向与请求转发的比较(HttpServletResponse.sendRedirect方法和RequestDispatcher.forward方法)