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的更多相关文章

  1. Struts2学习笔记(五)——Action访问Servlet API

    在Strut2中访问Servlet API有三种方式: 1.通过ActionContext访问Servlet API,推荐使用这种,但是这种方案它获取的不是真正的事Servlet API. 步骤: 1 ...

  2. Struts2笔记--Action访问Servlet API

    Web应用中通常需要访问的Servlet API就是HttpServletRequest.HttpSession和ServletContext,这三个接口分别代表JSP内置对象中的request.se ...

  3. struts2访问servlet API

    搭建环境: 引入jar包,src下建立struts.xml文件 项目配置文件web.xml. web.xml: <?xml version="1.0" encoding=&q ...

  4. struts2中访问servlet API

    Struts2中的Action没有与任何Servlet API耦合,,但对于WEB应用的控制器而言,不访问Servlet API几乎是不可能的,例如需要跟踪HTTP Session状态等.Struts ...

  5. Struts2访问Servlet API的三种方式

    有时我们需要用到Request, Response, Session,Page, ServletContext这些我们以前常用的对象,那么在Struts2中怎么样使用到这些对象呢,通常有三种方式. * ...

  6. struts2学习笔记(四)——访问Servlet的API&结果跳转&数据封装

    一.Struts2访问Servlet的API 前面已经对Struts2的流程执行完成了,但是如果表单中有参数如何进行接收?又或者我们需要向页面保存一些数据,又要如何完成呢?我们可以通过学习Struts ...

  7. struts2 笔记01 登录、常用配置参数、Action访问Servlet API 和设置Action中对象的值、命名空间和乱码处理、Action中包含多个方法如何调用

    Struts2登录 1. 需要注意:Struts2需要运行在JRE1.5及以上版本 2. 在web.xml配置文件中,配置StrutsPrepareAndExecuteFilter或FilterDis ...

  8. Struts2学习笔记三 访问servlet

    结果跳转方式 转发 <!-- 转发 --> <action name="Demo1Action" class="cn.itheima.a_result. ...

  9. Struts2学习二----------访问Servlet API

    © 版权声明:本文为博主原创文章,转载请注明出处 Struts2提供了三种方式去访问Servlet API -ActionContext -实现*Aware接口 -ServletActionConte ...

随机推荐

  1. tableZen maxHeight 解决方案 如果数据条数小于N,不进行高度设置,超过N条,直接设置高度,解决原生iview Table 对于右侧固定列,不能计算出正确数值的解决方案

    tableZen maxHeight 解决方案 如果数据条数小于N,不进行高度设置,超过N条,直接设置高度,解决原生iview Table 对于右侧固定列,不能计算出正确数值的解决方案 if (thi ...

  2. C++ json解决方案

    前段时间用到C++来封装com 因此从数据转换上我采用的Json来当两种语言的传递方式,现做下json的序列化与反序列化方案的总结: Rapidjson 文档地址:http://rapidjson.o ...

  3. Python基础 | 日期时间操作

    目录 获取时间 时间映射 格式转换 字符串转日期 日期转字符串 unixtime 时间计算 时间偏移 时间差 "日期时间数据"作为三大基础数据类型之一,在数据分析中会经常遇到. 本 ...

  4. DS博客作业02--栈和队列

    0.PTA得分截图 1.本周学习总结 1.1总结栈和队列内容 栈的存储结构及操作 栈的顺序存储结构 typedef struct { ElemType data[MaxSize]: int top: ...

  5. 使用WireShark进行网络流量安全分析

    WireShark的过滤规则 伯克利包过滤(BPF)(应用在wireshark的捕获过滤器上) ** 伯克利包过滤中的限定符有下面的三种:** Type:这种限定符表示指代的对象,例如IP地址,子网或 ...

  6. [深入学习C#] 匿名函数、委托和Lambda表达式

    匿名函数 匿名函数(Anonymous Function)是表示“内联”方法定义的表达式.匿名函数本身及其内部没有值或者类型,但是可以转换为兼容的委托或者表达式树类型(了解详情).匿名函数转换的计算取 ...

  7. 洛谷3834 hdu2665主席树模板,动态查询区间第k小

    题目链接:https://www.luogu.com.cn/problem/P3834 对于区间查询第k小的问题,在区间数量达到5e5的时候是难以用朴素数据结构实现的,这时候主席树就应运而生了,主席树 ...

  8. Servlet(二)----注解配置

    ##  Servlet3.0 *  好处: *  支持注解配置.可以不需要web.xml了. *  步骤: 1.创建JavaEE项目,选择Servlet的版本3.0以上,可以不创建web.xml 2. ...

  9. xgboost安装与原理

    1.xgboost库的安装 先在网址https://www.lfd.uci.edu/~gohlke/pythonlibs/#xgboost 中下载whl文件,注意一定要下载跟自己当前安装Python版 ...

  10. SpringBoot安装与配置

    1.环境准备 1.1.Maven安装配置 Maven项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的项目管理工具软件. 下载Maven可执行文件 cd /usr/local ...