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. token iviewAdmin + php 登录验证解决方案

    思路: php 开启 Session 登录时 生成token,前端存下,然后每次走接口 验证下Session里的token和前端发过来的token是否一样. 遇到问题:后端 每次PHP Session ...

  2. spring Boot登录验证之验证码 邮箱

    一 验证码 登录login.jsp <%@ page contentType="text/html;charset=UTF-8" language="java&qu ...

  3. MySQL数据备份及还原(一)

    关于删库跑路的事故现在已经屡见不鲜了,数据备份的必要性是企业数据管理极其重要的一项工作.关于数据备份.恢复也有很多场景及方法,本系列也会将主要的几种工具通过案例进行演示. 本系列将从逻辑备份及恢复开始 ...

  4. linux无文件执行— fexecve 揭秘

    前言 良好的习惯是人生产生复利的有力助手. 继续2020年的flag,至少每周更一篇文章. 无文件执行 之前的文章中,我们讲到了无文件执行的方法以及混淆进程参数的方法,今天我们继续讲解一种linux上 ...

  5. 玩转控件:扩展Dev中SimpleButton

    何为扩展,顾名思义,就是在原有控件属性.事件的基础上拓展自己需要或实用的属性.事件等等.或者可以理解为,现有的控件已经不能完全满足我(的需求)了.好的扩展会使控件更加完善,实用,好用.不好的扩展,说白 ...

  6. 图数据库 Nebula Graph TTL 特性

    导读 身处在现在这个大数据时代,我们处理的数据量需以 TB.PB, 甚至 EB 来计算,怎么处理庞大的数据集是从事数据库领域人员的共同问题.解决这个问题的核心在于,数据库中存储的数据是否都是有效的.有 ...

  7. 手把手教你用GoEasy实现Websocket IM聊天

    经常有朋友问起GoEasy如何实现IM,今天就手把手的带大家从头到尾用GoEasy实现一个完整IM聊天,全套代码已经放在了github. 今日的前端技术发展可谓百花争鸣,为了确保本文能帮助到使用任何技 ...

  8. Django之Ajax传输数据

    MTV与MVC模型 MTV与MVC都是模型,只不过MTV是django自己定义的,具体看一下他们的意思 MTV模型(django) M:模型层(models.py) T:templates文件夹 V: ...

  9. 【数据结构和算法】001 单链表 LinkedList

    一.单链表(LinkedList)介绍和内存布局 链表是有序的列表,它在内存中的实际存储结构如下: 看上去虽然无序,但他是靠灭个链表节点元素的地址和next域来分清首尾相连的顺序,如下图所示,由头指针 ...

  10. windows常用系统命令

    dir指定要列出的驱动器.目录和/或文件 显示当前路径下的所有文件的绝对路径,包含子文件夹中的内容 dir /b / s /o:n /a:a /b 表示去除摘要信息,且顶格显示完整路径 /s 表示枚举 ...