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 ...
随机推荐
- SpringBoot2整合Redis多数据源
配置文件属性 spring: redis: database: 1 host: 192.168.50.144 port: 6379 password: timeout: 600 #Springboot ...
- Who Gets the Most Candies? POJ - 2886(线段树单点更新+区间查询+反素数)
预备知识:反素数解析 思路:有了反素数的解法之后就是线段树的事了. 我们可以用线段树来维护哪些人被淘汰,哪些人没被淘汰,被淘汰的人的位置,没被淘汰的人的位置. 我们可以把所有人表示为一个[1,n]的区 ...
- 曹工说Spring Boot源码(24)-- Spring注解扫描的瑞士军刀,asm技术实战(上)
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...
- Linux下安装MySQL的tar.gz包
以root用户登录待安装的服务器. 上传软件包并解压. 以root用户通过sftp/ftp工具上传“mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz”软件包到“/o ...
- hdu2612 又是迷宫系列
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/2612/ 题意:有两个人在地图上不同的位置,地图上由若干个餐厅,求两人能同时到达一个餐厅所用最少的总时间. 代码如 ...
- 洛谷 P1047 校门外的树 题解
Case 1. 本题其实不难,直接模拟就可以了.时间复杂度: \(O(L \times M)\) Case 2. 考虑一个简单的增强:把原来的: \[L \leq 10^4,M \leq 10^2 \ ...
- Electron 踩坑记录- require is not defined
从 github 上拉取 electron-sample-apps 后,运行 electron printing, 出现如下报错信息 解决方案 在 main.js 中增加 nodeIntegratio ...
- python之面向对象三大特性: 继承(单继承)
什么是继承 专业角度: B 继承 A类, B就叫做A的子类,派生类, A叫做B的父类,基类,超类. B类以及B类的对象使用A类的所有的属性以及方法. 字面意思: 继承就是继承父母所有的资产 class ...
- Java生鲜电商平台-电商中"再来一单"功能架构与详细设计(APP/小程序)
Java生鲜电商平台-电商中"再来一单"功能架构与详细设计(APP/小程序) 说明:在实际的业务场景中(无论是TO B还是TO C)不管是休闲食品.餐饮.水果.日用百货.母婴等高频 ...
- IntelliJ Idea 中文乱码问题
首先,Idea真的是一款很方便的开发工具,但是关于中文乱码这个问题我不得不吐槽,这个编码也弄得这么麻烦干嘛呀...下面就说一下怎么解决中文乱码问题: 1.首先是编辑器的乱码,这个很好解决,file-& ...