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. flex 居中

    display: flex; justify-content: space-between; align-items: center;

  2. 【股票盯盘软件】01_程序员炒股之开发一款极简风格的股票盯盘软件StockDog_V1.0.0.1

    1.前言 话说最近一段时间受疫情的影响,股市各种妖魔横行.本人作为一个入股市不满三年的小韭菜,就有幸见证了好几次历史,也是满心惊喜,就权当是接受资本市场的再教育了吧. 小韭菜的炒股方法其实很简单,这两 ...

  3. 解决tinyint映射成boolean/byte的问题

    前言 最近受疫情的影响,公司要做一个类似一码通的系统为客户服务.由我来进行表的设计.创建表之后需要逆向生成Java的entity.mapper.mapper.xml.由于我在数据库中定义了大量 tin ...

  4. Mysql - 删除表时出现: Cannot delete or update a parent row: a foreign key constraint fails

    现象 MySQL在删除一张表时出现 ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint ...

  5. Scapy编写UDP扫描脚本

    脚本内容如下: from scapy.all import * import optparse import threading def scan(target,port): pkt=IP(dst=t ...

  6. [离散化+树状数组]CodeForces - 652D Nested Segments

    Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  7. 「面试指南」JS数组Array常用算法,Array算法的一般解答思路

    先看一道面试题 在 LeetCode 中有这么一道简单的数组算法题: // 给定一个整数数组 nums 和一个目标值 target, // 请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下 ...

  8. Selenium系列(十一) - 针对两种上传文件方式的实现方案

    如果你还想从头学起Selenium,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1680176.html 其次,如果你不懂前端基础知识, ...

  9. 爬虫&Selenium&ChromeDriver

    一.Selenium selenium是什么 Selenium [1] 是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE(7, ...

  10. 5L-链表导论心法

    链表是比数组稍微复杂一点的数据结构,也是两个非常重要与基本的数据结构.如果说数组是纪律严明排列整齐的「正规军」那么链表就是灵活多变的「地下党」. 关注公众号 MageByte,有你想要的精彩内容. 链 ...