Session的方法getSession() 与 getSession(boolean para)区别

getSession(boolean para)返回当前http会话,如果不存在,则创建一个新的会话
getSession() 调用getSession(true)的简化版

【官方解释】
getSession
public HttpSession getSession(boolean create)
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
译:
getSession(boolean create)意思是返回当前reqeust中的HttpSession ,如果当前reqeust为false,并且当前没有HttpSession,那么久返回为null,如果create为true,没有Session时就会创建一个新的Session;
简而言之:
HttpServletRequest.getSession(ture) 等同于 HttpServletRequest.getSession()
HttpServletRequest.getSession(false) 等同于 如果当前Session没有就为null;

时间: 2024-08-10 15:10:49

Session的方法getSession() 与 getSession(boolean para)区别的相关文章

【JAVA学习】struts2的action中使用session的方法

尊重版权:http://hi.baidu.com/dillisbest/item/0bdc35c0b477b853ad00efac 在Struts2里,如果需要在Action中使用session,可以通过下面两种方式得到1.通过ActionContext class中的方法getSession得到2.Action实现org.apache.struts2.interceptor.SessionAware接口的方式来对session进行操作 下面先看一个采用第一种方式,在action中得到sessi

【转】于request.getSession(true/false/null)的区别

http://blog.csdn.net/gaolinwu/article/details/7285783 关于request.getSession(true/false/null)的区别 一.需求原因 现实中我们经常会遇到以下3中用法: HttpSession session = request.getSession(); HttpSession session = request.getSession(true); HttpSession session = request.getSessi

getHibernateTemplate.find()和session.createQuery()方法总结

Spring中常用的hql查询方法(getHibernateTemplate().find()) 一.find(String queryString); 示例:this.getHibernateTemplate().find("from bean.User"); 返回所有User对象 二.find(String queryString , Object value); 示例:this.getHibernateTemplate().find("from bean.User u

清空session的方法

清空session的方法,常用来注销的时候清空所有的session. 方法一: Enumeration e=session.getAttributeNames(); while(e.hasMoreElements()){ String sessionName=(String)e.nextElement(); System.out.println("存在的session有:"+sessionName); session.removeAttribute(sessionName); } 方法

第二节 hibernate session基本方法介绍

Hibernate session 对象是和数据库建立连接的对象,session中包含了许多操作数据库的方法,下面一一描述. 1 保存对象到数据库 session.save(); 按照第一节的配置,主键的生成策略为native(数据库自动生成主键),由于数据库使用的是mysql 5,所以是自增的主键生成方式.保存对象时并不需要设置id属性. @Test public void testSave() { try { User user = new User(); user.setBirthday(

【翻译自mos文章】找到持有library cache lock session的方法

找到持有library cache lock session的方法 参考自: How to Find which Session is Holding a Particular Library Cache Lock (文档 ID 122793.1) 其实就是两种方法: 一.Systemstate Analysis 此处不做翻译,原文转载 Systemstate event will create a tracefile containing detailed information on eve

asp.net网站防恶意刷新的Cookies与Session解决方法

本文实例讲述了asp.net网站防恶意刷新的Cookies与Session解决方法,是WEB程序设计中非常实用的技巧.分享给大家供大家参考.具体实现方法如下: Session版实现方法: public double time; public const int freetime = 1;//防刷冰冻时间间隔,当前为1秒 #region 防恶意刷新 if (Session.SessionID == null) {   Response.End(); } else if (Session["sion

ASP.NET ASHX中获得Session的方法

1-在 aspx和aspx.cs中,都是以Session["xxx"]="aaa"和aaa=Session["xxx"].ToString()进行读写. 而在ashx中,Session都要使用context.Session,读写方法是这样的: context.Session["xxx"]="aaa"和aaa=context.Session["xxx"].ToString() 2-在ash

Cookie禁用使用Session值方法

Cookie禁用使用Session值方法: 首先:        PHP中的Session在默认情况下是使用客户端的Cookie来保存Session ID的,所以当客户端的cookie出现问题的时候就会影响Session了.必须注意的是:Session不一定必须依赖Cookie,这也是Session相比Cookie的实用之处. 当客户端的Cookie被禁用或出现问题时,PHP会自动把Session ID携带在在URL中,这样再通过Session ID就能跨页使用Session变量. 不是自带就能