Struts2-学习笔记系列(4)-访问servlet api
5.1通过actioncontext:
public String execute() throws Exception {
ActionContext ctx = ActionContext.getContext();
// 通过ActionContext访问application范围的属性值
Integer counter = (Integer) ctx.getApplication().get("counter");
if (counter == null) {
counter = 1;
} else {
counter = counter + 1;
}
// 通过ActionContext设置application范围的属性
ctx.getApplication().put("counter", counter);
// 通过ActionContext设置session范围的属性
ctx.getSession().put("user", getUser());
if (getUser().equals("zcx")) {
// 通过ActionContext设置request范围的属性
ctx.put("tip", "欢迎登录");
return SUCCESS;
}
// 通过ActionContext设置request范围的属性
ctx.put("tip", "登录失败");
return ERROR;
}
取数据:注意写在HTML页面的OGNL表达式语法
${applicationScope.counter}
${sessionScope.user}
${requestScope.tip}
5.2实现servletcontextaware、servletrequestaware、servletresponseaware
实现ServletResponseAware 设置cookie
public class LoginAction implements Action, ServletResponseAware {
private String user;
private String pwd;
private String tip;
private HttpServletResponse response;
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String execute() throws Exception {
ActionContext ctx = ActionContext.getContext();
// 通过ActionContext访问application范围的属性值
Integer counter = (Integer) ctx.getApplication().get("counter");
if (counter == null) {
counter = 1;
} else {
counter = counter + 1;
}
// 通过ActionContext设置application范围的属性
ctx.getApplication().put("counter", counter);
// 通过ActionContext设置session范围的属性
ctx.getSession().put("user", getUser());
if (getUser().equals("zcx")) {
// 通过response添加Cookie
Cookie c = new Cookie("user", getUser());
c.setMaxAge(60 * 60);
response.addCookie(c);
// 通过ActionContext设置request范围的属性
ctx.put("tip", "服务器提示:您已经成功的登录");
return SUCCESS;
}
// 通过ActionContext设置request范围的属性
ctx.put("tip", "登录失败");
return ERROR;
}
public String getTip() {
return tip;
}
public void setTip(String tip) {
this.tip = tip;
}
@Override
public void setServletResponse(HttpServletResponse httpServletResponse) {
this.response = response;
}
}
5.3使用servletactioncontext
public class LoginAction implements Action {
private String user;
private String pwd;
private String tip;
private HttpServletResponse response;
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String execute() throws Exception {
ActionContext ctx = ActionContext.getContext();
// 通过ActionContext访问application范围的属性值
Integer counter = (Integer) ctx.getApplication().get("counter");
if (counter == null) {
counter = 1;
} else {
counter = counter + 1;
}
// 通过ActionContext设置application范围的属性
ctx.getApplication().put("counter", counter);
// 通过ActionContext设置session范围的属性
ctx.getSession().put("user", getUser());
if (getUser().equals("zcx")) {
// 通过response添加Cookie
Cookie c = new Cookie("user", getUser());
c.setMaxAge(60 * 60);
ServletActionContext.getResponse().addCookie(c);
// 通过ActionContext设置request范围的属性
ctx.put("tip", "服务器提示:您已经成功的登录");
return SUCCESS;
}
// 通过ActionContext设置request范围的属性
ctx.put("tip", "登录失败");
return ERROR;
}
public String getTip() {
return tip;
}
public void setTip(String tip) {
this.tip = tip;
}
}
Struts2-学习笔记系列(4)-访问servlet api的更多相关文章
- Struts2学习笔记(五)——Action访问Servlet API
在Strut2中访问Servlet API有三种方式: 1.通过ActionContext访问Servlet API,推荐使用这种,但是这种方案它获取的不是真正的事Servlet API. 步骤: 1 ...
- Struts2笔记--Action访问Servlet API
Web应用中通常需要访问的Servlet API就是HttpServletRequest.HttpSession和ServletContext,这三个接口分别代表JSP内置对象中的request.se ...
- struts2访问servlet API
搭建环境: 引入jar包,src下建立struts.xml文件 项目配置文件web.xml. web.xml: <?xml version="1.0" encoding=&q ...
- struts2中访问servlet API
Struts2中的Action没有与任何Servlet API耦合,,但对于WEB应用的控制器而言,不访问Servlet API几乎是不可能的,例如需要跟踪HTTP Session状态等.Struts ...
- Struts2访问Servlet API的三种方式
有时我们需要用到Request, Response, Session,Page, ServletContext这些我们以前常用的对象,那么在Struts2中怎么样使用到这些对象呢,通常有三种方式. * ...
- struts2学习笔记(四)——访问Servlet的API&结果跳转&数据封装
一.Struts2访问Servlet的API 前面已经对Struts2的流程执行完成了,但是如果表单中有参数如何进行接收?又或者我们需要向页面保存一些数据,又要如何完成呢?我们可以通过学习Struts ...
- struts2 笔记01 登录、常用配置参数、Action访问Servlet API 和设置Action中对象的值、命名空间和乱码处理、Action中包含多个方法如何调用
Struts2登录 1. 需要注意:Struts2需要运行在JRE1.5及以上版本 2. 在web.xml配置文件中,配置StrutsPrepareAndExecuteFilter或FilterDis ...
- Struts2学习笔记三 访问servlet
结果跳转方式 转发 <!-- 转发 --> <action name="Demo1Action" class="cn.itheima.a_result. ...
- Struts2学习二----------访问Servlet API
© 版权声明:本文为博主原创文章,转载请注明出处 Struts2提供了三种方式去访问Servlet API -ActionContext -实现*Aware接口 -ServletActionConte ...
随机推荐
- 使用C#+EmguCV处理图像入门(一)
首先我们先了解一下该库的一些相关信息 OpenCV(Open Source Computer Vision Library)是一个(开源免费)发行的跨平台计算机视觉库,可以运行在Linux.Windo ...
- Map-->HashMap练习(新手)
//导入的包.import java.util.*;//创建的一个类.public class zylx1 { //公共静态的主方法. public static void main(String[] ...
- 大数据软件安装之Azkaban(任务调度)
一.安装部署 1.安装前准备 1)下载地址:http://azkaban.github.io/downloads.html 2)将Azkaban Web服务器.Azkaban执行服务器.Azkaban ...
- Spring框架——继承 - 依赖 - 命名空间
Spring 继承 子 bean 可以继承⽗ bean 的属性值. <bean id="user" class="com.sunjian.entity.User&q ...
- django之forms组件,cookie&session
forms组件 先自己实现注册功能,并且对用户输入的信息加限制条件如果用户输入的信息不符合条件,前端展示报错信息 from django.shortcuts import render,HttpRes ...
- Javascript之实现页面倒计时效果
本文将从需求实现的角度,逐步讲解如何在页面上实现倒计时效果,其中部分涉及到的知识会做拓展讲解,最后将所有代码封装,适用于不同情况下倒计时功能的实现. 效果图 一.分析需求 要实现倒计时效果,可拆解为以 ...
- 粒子群优化算法(PSO)之基于离散化的特征选择(FS)(四)
作者:Geppetto 前面我们介绍了特征选择(Feature Selection,FS)与离散化数据的重要性,介绍了PSO在FS中的重要性和一些常用的方法.FS与离散化的背景,介绍了EPSO与PPS ...
- 使用node.js中遇到的一些小bug
1.BUG Cannot set headers after they are sent to the client 解决:即发出一次请求得到两次或以上的回应时会出现此警告,此时注意查看再在一些条件下 ...
- Dome 多人人脸识别 face_recognition
Dome 多人人脸识别 face_recognition 注意 face_recognition 依赖 face_recognition_models 中文字体文件需要自己下载 1.多人人脸识别 # ...
- LayUI制作日历工作记录簿
标题不知道该如何取,大概就是用Lay UI的Table,制作一个日历,在日历上可以添加每天的工作简记录.记录下LayUI Table的一些用法,一些值得探索的地方在于日历生成后,给周末加背景色,当天加 ...