首页 技术 正文
技术 2022年11月16日
0 收藏 380 点赞 5,006 浏览 1548 个字

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,即通过键找到值。例如你的名字和你的人的关系,只要一叫你的名字,你就会喊到,通过你的名字来找你的人,简单说这就是键值对的概念。
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,565
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905