protected ServletContext getServletContext() { return ServletActionContext.getServletContext();
}

protected HttpSession getSession(boolean paramBoolean) { return ServletActionContext.getRequest().getSession(paramBoolean);
}

protected HttpSession getSession() { return ServletActionContext.getRequest().getSession();
}

protected HttpServletRequest getRequest() { return ServletActionContext.getRequest();
}

protected HttpServletResponse getResponse() { return ServletActionContext.getResponse();
}
HttpServletRequest request = ServletActionContext.getRequest();

getSession()是返回当前用户的会话对象,参数的区别在于     
  参数为true,则如果“当前用户的会话对象”为空(第一次访问时)则创建一个新的会话对象返回   
  参数为false,则如果“当前用户的会话对象”为空,则返回null(即不自动创建会话对象)
注意request.getSession() 等同于 request.getSession(true),除非我们确认session一定存在或者sesson不存在时明确有创建session的需要,否则请尽量使用request.getSession(false)。

request.setAttribute一次传参数,速度快,缺点是参数只能取一次。
request.getSession().setAttribute保存参数,将参数放入后主要不清,就一直在,随时可以取来用。缺点是比较占用资源。
setAttribute(string name, string value):增加一个指定名称和值的新属性,或者把一个现有的属性设定为指定的值。

1、request.getSession()可以帮你得到HttpSession类型的对象,通常称之为session对象session对象的作用域为一次会话,通常浏览器不关闭,保存的值就不会消失,当然也会出现session超时。服务器里面可以设置session的超时时间,web.xml中有一个session time out的地方,tomcat默认为30分钟
2、session.setAttribute("key",value);是session设置值的方法,原理同java中的HashMap的键值对,意思也就是key现在为“user”;存放的值为userName,userName应该为一个String类型的变量吧?看你自己的定义。
3、可以使用session.getAttribute("key");来取值,以为着你能得到userName的值。
4、注意:getAttribute的返回值类型是Object,需要向下转型,转成你的userName类型的,简单说就是存什么,取出来还是什么。
5、setAttribute和getAttribute就是基于HashMap的put方法和get方法实现的,一般叫键值对或者key-value,即通过键找到值。例如你的名字和你的人的关系,只要一叫你的名字,你就会喊到,通过你的名字来找你的人,简单说这就是键值对的概念。

如何获得 request, "request.getSession(true).setAttribute("a",a);"与“request.setAttribute("a",a);”区别的更多相关文章

  1. 转:request.getSession(true)和request.getSession(false)的区别

    1.转自:http://wenda.so.com/q/1366414933061950?src=150 概括: request.getSession(true):若存在会话则返回该会话,否则新建一个会 ...

  2. 关于request.getsession(true|false)

    request.getSession(true):若存在会话则返回该会话,否则新建一个会话.request.getSession(false):若存在会话则返回该会话,否则返回NULL

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

    http://blog.csdn.net/gaolinwu/article/details/7285783 关于request.getSession(true/false/null)的区别 一.需求原 ...

  4. request.getSession(true)和request.getSession(false)的区别

    request.getSession(true)和request.getSession(false)的区别   request.getSession(true):若存在会话则返回该会话,否则新建一个会 ...

  5. request.getSession(true/false)的区别

    javax.servlet.http.HttpServletRequest接口有两个方法:getSession(boolean)和getSession(). 具体什么区别,跟踪源码分析下,先摆出结论: ...

  6. request.getSession()、reqeust.getSession(false)和request.getSession(true)

    getSession()/getSession(true):当session存在时返回该session,否则新建一个session并返回该对象 getSession(false):当session存在 ...

  7. scrapy-yield scrapy.Request()不执行、失效、Filtered offsite request to错误 [转]

    scrapy错误:yield scrapy.Request()不执行.失效.Filtered offsite request to错误.首先我们在Request()方法里面添加这么一个东东: yiel ...

  8. 此时servlet中的request和我们在页面jsp中的request 是同一个request.

    在tomcat容器启动的时候,jsp页面的内置对象request,response,同样是依赖于tomcat容器中的servlet-api.jar包,这个jar包和我们在web项目中的jar包是一样的 ...

  9. request.getSession().setAttribute("",..)和request.setAttribute("",...)的差别

    request.getSession.setAttribute()是获得当前会话的session,然后再setAttribute到session里面去,有效范围是session而不是request. ...

随机推荐

  1. CSS超出部分显示省略号…代码

    让DIV,LI等元素超出部分文字用省略号…显示. 示例: 兼容IE/Firefox/Chrome 代码: display:block;white-space:nowrap; overflow:hidd ...

  2. SQLite手工注入方法小结

    SQLite 官网下载:www.sqlite.org/download.html sqlite管理工具:http://www.yunqa.de/delphi/products/sqlitespy/in ...

  3. union all 取代 select中的case when 提高查询效率

    首先union all不是适用于所有情况,其次,case when的可读性肯定要强.所以在不是特别在意性能的情况下, 可以考虑用case when. 再者,不是所有情况下的union all都要比ca ...

  4. 黑马程序员——JAVA基础之IO流缓冲区,转换流,字节流

    ------- android培训.java培训.期待与您交流! ---------- 字符流的缓冲区        缓冲区的出现提高了对数据的读写效率. 对应类 •  BufferedWriter ...

  5. Android学习四:数据库操作

    1前言 android中使用SQLite作为数据库,在进行相关的开发的时候不需要导入包.SQLite起符合SQL标准,也有自己的一些特性,是一个轻量级的数据库. 2代码 简单的数据库类封装 packa ...

  6. 20151217JS便签

    JS便签: 根据一个数值来改变Repeater行数的颜色 <script type="text/javascript"> var query = document.ge ...

  7. MVC 全局异常处理及禁用显示头

    MVC网站的global.asax中的Application_Start方法里,有这样一段代码: public class MvcApplication : System.Web.HttpApplic ...

  8. jquery获得图片的真实大小

    $(function(){ var imgSrc = $("#image").attr("src"); getImageWidth(imgSrc,functio ...

  9. ORA-12154: TNS:could not resolve the connect identifier specified

    场景: .Net程序无法连接到数据库 现象: 2015/8/26 11:02:03 ORA-12154: TNS:could not resolve the connect identifier sp ...

  10. STM32F4 SPI2初始化及收发数据【使用库函数】

    我的STM32F4 Discovery上边有一个加速度传感器LIS302DL.在演示工程中,ST的工程师使用这个传感器做了个很令人羡慕的东西:解算开发板的姿态.当开发板倾斜时候,处于最上边的LED点亮 ...