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 ...
随机推荐
- Log4j不写入日志文件排错记录
背景: 之前用 log4j一直设置的输出到控制台.今天由于job任务出现了异常,因为是异步的,没办法在控制台看错误信息了,于是乎决定把日志打印到文件里面.然后就找了篇博客配置了下.但是配置完后,怎么也 ...
- vscode回车补全代码
VsCode设置回车补全代码而不换行 有一部分人不习惯用tab键补全代码,我就是其中之一,习惯了回车补全的我决定设置一波,网上找了很多, 没找到比较详细的,所以自己写一个 有一个叫keybinding ...
- 阿里云服务器安装Docker
在阿里云服务器上安装Docker,服务器的系统是CentOS 7.6, 所以可以看官方Docker安装文档:https://docs.docker.com/install/linux/docker-c ...
- Android ListView显示访问WebServices返回的JSON结果
1.WebServices的返回结果 2.ListView内容布局代码 <?xml version="1.0" encoding="utf-8"?> ...
- 【codeforces】Educational Codeforces Round 80 D. Minimax Problem——二分+二进制处理
题目链接 题目大意 有n个维度为m的向量,取其中两个进行合并,合并时每个维度取两者之间的较大者,得到的新的向量中,维度值最小者最大为多少 分析 首先最需要注意的是m的取值,m最大只有8 那么我们可以二 ...
- Trie树-0/1字典树-DFS-1624. 最大距离
2020-03-18 20:45:47 问题描述: 两个二进制串的距离是去掉最长公共前缀的长度之和.比如: 1011000和1011110的最长公共前缀是1011, 距离就是 len("00 ...
- UVA11987 Almost Union-Find 并查集的节点删除
题意: 第一行给出一个n,m,表示 n个集合,里面的元素为1~n,下面有m种操作,第一个数为 1 时,输入a,b 表示a,b 两个集合合并到一起,第一个数为 2 时,输入a,b表示将 a 从他原来的集 ...
- Python python 五种数据类型--字典
# 定义一个字典 var1 = {'a':20,'b':40}; var2 = dict(); print(type(var1)) print(type(var2)) # 长度 length = le ...
- js 的 new 干了什么
- php _weakup()反序列化漏洞
概念&原理 序列化就是使用 serialize() 将对象用字符串的方式进行表示: 反序列化是使用 unserialize() 将序列化的字符串构造成相应的对象,为序列化的逆过程. 序列化的对 ...