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 ...
随机推荐
- CSS每日学习笔记(1)
7.30.2019 1.CSS 文本属性 属性 描述 color 设置文本颜色 direction 设置文本方向. line-height 设置行高. letter-spacing 设置字符间距. t ...
- 大数据软件安装之Hadoop(Apache)(数据存储及计算)
大数据软件安装之Hadoop(Apache)(数据存储及计算) 一.生产环境准备 1.修改主机名 vim /etc/sysconfig/network 2.修改静态ip vim /etc/udev/r ...
- 从零开始学习R语言(五)——数据结构之“列表(List)”
本文首发于知乎专栏:https://zhuanlan.zhihu.com/p/60141740 也同步更新于我的个人博客:https://www.cnblogs.com/nickwu/p/125678 ...
- Mol Cell Proteomics. | Prediction of LC-MS/MS properties of peptides from sequence by deep learning (通过深度学习技术根据肽段序列预测其LC-MS/MS谱特征) (解读人:梅占龙)
通过深度学习技术根据肽段序列预测其LC-MS/MS谱特征 解读人:梅占龙 质谱平台 文献名:Prediction of LC-MS/MS properties of peptides from se ...
- [BJDCTF 2nd]old-hack
进入首页: 首页告诉了我们是thinkphp5的漏洞. 知道了是哪个版本的话就搜一搜喽:最后发现是thinkphp5.0.23的命令执行 payload_1:查看根目录文件,发现flag位置 http ...
- hGame2020第二周第一题题解
Description: Cosmos通过两个小时速成了PHP+HTML,他信心满满的写了一个博客,他说要从博客后台开始......(flag在根目录, 禁止使用任何扫描器) Challenge Ad ...
- vue项目创建与使用
目录 复习 Vue项目环境搭建 Vue项目创建 pycharm配置并启动vue项目 vue项目目录结构分析 vue组件(.vue文件) 全局脚本文件main.js(项目入口) 改写 vue项目启动生命 ...
- 介绍 Seq2Seq 模型
2019-09-10 19:29:26 问题描述:什么是Seq2Seq模型?Seq2Seq模型在解码时有哪些常用办法? 问题求解: Seq2Seq模型是将一个序列信号,通过编码解码生成一个新的序列信号 ...
- 模型压缩一半,精度几乎无损,TensorFlow推出半精度浮点量化工具包,还有在线Demo...
近日,TensorFlow模型优化工具包又添一员大将,训练后的半精度浮点量化(float16 quantization)工具. 有了它,就能在几乎不损失模型精度的情况下,将模型压缩至一半大小,还能改善 ...
- 使用wrd2vec构建推荐系统
概览 完整的代码可以从这里下载: https://github.com/prateekjoshi565/recommendation_system/blob/master/recommender_2. ...