如何获得 request, "request.getSession(true).setAttribute("a",a);"与“request.setAttribute("a",a);”区别
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);”区别的更多相关文章
- 转:request.getSession(true)和request.getSession(false)的区别
1.转自:http://wenda.so.com/q/1366414933061950?src=150 概括: request.getSession(true):若存在会话则返回该会话,否则新建一个会 ...
- 关于request.getsession(true|false)
request.getSession(true):若存在会话则返回该会话,否则新建一个会话.request.getSession(false):若存在会话则返回该会话,否则返回NULL
- 【转】于request.getSession(true/false/null)的区别
http://blog.csdn.net/gaolinwu/article/details/7285783 关于request.getSession(true/false/null)的区别 一.需求原 ...
- request.getSession(true)和request.getSession(false)的区别
request.getSession(true)和request.getSession(false)的区别 request.getSession(true):若存在会话则返回该会话,否则新建一个会 ...
- request.getSession(true/false)的区别
javax.servlet.http.HttpServletRequest接口有两个方法:getSession(boolean)和getSession(). 具体什么区别,跟踪源码分析下,先摆出结论: ...
- request.getSession()、reqeust.getSession(false)和request.getSession(true)
getSession()/getSession(true):当session存在时返回该session,否则新建一个session并返回该对象 getSession(false):当session存在 ...
- scrapy-yield scrapy.Request()不执行、失效、Filtered offsite request to错误 [转]
scrapy错误:yield scrapy.Request()不执行.失效.Filtered offsite request to错误.首先我们在Request()方法里面添加这么一个东东: yiel ...
- 此时servlet中的request和我们在页面jsp中的request 是同一个request.
在tomcat容器启动的时候,jsp页面的内置对象request,response,同样是依赖于tomcat容器中的servlet-api.jar包,这个jar包和我们在web项目中的jar包是一样的 ...
- request.getSession().setAttribute("",..)和request.setAttribute("",...)的差别
request.getSession.setAttribute()是获得当前会话的session,然后再setAttribute到session里面去,有效范围是session而不是request. ...
随机推荐
- LintCode Binary Tree Inorder Traversal
Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...
- 论文笔记之:Deep Recurrent Q-Learning for Partially Observable MDPs
Deep Recurrent Q-Learning for Partially Observable MDPs 摘要:DQN 的两个缺陷,分别是:limited memory 和 rely on b ...
- php 单引号 双引号 ,php字符串/ hmtl / 数据库显示/ 及php的几个转化函数
* 以单引号为定界符的php字符串,支持两个转义\'和\\* 以双引号为定界符的php字符串,支持下列转义(\'会直接输出\' ,也会转义 \\): \n 换行(LF 或 ASCII 字符 0x ...
- gradle 命令
gradle -v gradle clean gralde build gradle assembleDebug gradle assembleRelease gradle clean --refre ...
- java之main
Java中用户向系统传递参数的三种基本方式 main方法 在Java中,main()方法是Java应用程序的入口方法,也就是说,程序在运行的时候,第一个执行的方法就是main()方法,这个方法和其他的 ...
- 抽象类和接口 static final finalize
抽象类 任何含有一个或多个抽象方法的类都必须声明成抽象类,用关键字abstract. 抽象类没有对象,即一个抽象类不能用new来直接实例化. 不能定义抽象构造方法或抽象静态方法.(但是可以创建父类引用 ...
- ASP.NET SignalR 与 LayIM2.0 配合轻松实现Web聊天室(十三)之附加功能-自定义皮肤
前言 本篇要讲的算是一个layim代码功能扩充.在原来的laim中已经有自带的换肤功能,而且在skin配置中,你可以添加自己想要的皮肤图片路径.这些内容在接下来都不会涉及,本篇要讲的是自定义皮肤功能, ...
- ecstore与淘宝sdk的autoload加载顺序问题
ecstore使用spl_autoload_register实现类的自动加载,这个很大的方便我们不用每次都要手动的去include一些类和函数.不过这样会导致一些问题,比如说,有一些extension ...
- 【转】maven核心,pom.xml详解
感谢如下博主: http://www.cnblogs.com/qq78292959/p/3711501.html maven核心,pom.xml详解 什么是pom? pom作为项目对象模型.通过 ...
- python标准库xml.etree.ElementTree的bug
使用python生成或者解析xml的方法用的最多的可能就数python标准库xml.etree.ElementTree和lxml了,在某些环境下使用xml.etree.ElementTree更方便一些 ...