一、直接实现Action接口或集成ActionSupport类(推荐)

public class HelloAction implements Action {

  @Override
  public String execute() throws Exception {
    Map<String, Object> requestScope = (Map<String, Object>) ActionContext.getContext().get("request");
    Map<String, Object> sessionScope = ActionContext.getContext().getSession();
    Map<String, Object> applicationScope = ActionContext.getContext().getApplication();
    Map<String, Object> params = ActionContext.getContext().getParameters();
    requestScope.put("name", "request");
    sessionScope.put("name", "session");
    applicationScope.put("name", "application");
    String[] strs = (String[]) params.get("name");
    System.out.println(Arrays.toString(strs));
    return SUCCESS;
  }

}

二、使用ServletActionContext对象获得原生的Servlet对象

public class HelloAction implements Action {

  @Override

  public String execute1() {
    HttpServletRequest request = ServletActionContext.getRequest();
    HttpServletResponse response = ServletActionContext.getResponse();
    HttpSession session = request.getSession();
    ServletContext context = ServletActionContext.getServletContext();
    return SUCCESS;
  }

}

三、

public class HelloAction extends ActionSupport implements ServletRequestAware, ServletResponseAware, SessionAware {
  private HttpServletRequest request;
  private HttpServletResponse response;
  private Map<String, Object> session;

  @Override
  public void setSession(Map<String, Object> session) {
    // TODO Auto-generated method stub
    this.session = session;
  }

  @Override
  public void setServletResponse(HttpServletResponse response) {
    // TODO Auto-generated method stub
    this.response = response;
  }

  @Override
  public void setServletRequest(HttpServletRequest request) {
    // TODO Auto-generated method stub
    this.request = request;
  }

  @Override
  public String execute() throws Exception {

    return SUCCESS;
  }

}

使用servletAPI三种方式简单示例的更多相关文章

  1. Python实现定时执行任务的三种方式简单示例

    本文实例讲述了Python实现定时执行任务的三种方式.分享给大家供大家参考,具体如下: 1.定时任务代码 import time,os,sched schedule = sched.scheduler ...

  2. RocketMQ(6)---发送普通消息(三种方式)

    发送普通消息(三种方式) RocketMQ 发送普通消息有三种实现方式:可靠同步发送.可靠异步发送.单向(Oneway)发送. 注意 :顺序消息只支持可靠同步发送. GitHub地址: https:/ ...

  3. 【深入Struts2】获取ServletAPI的三种方式

    一:获取servletAPI的三种方法 在传统的Web开发中,经常会用到Servlet API中的HttpServletRequest.HttpSession和ServletContext.Strut ...

  4. Django文件上传三种方式以及简单预览功能

    主要内容: 一.文件长传的三种方式 二.简单预览功能实现 一.form表单上传 1.页面代码 <!DOCTYPE html> <html lang="en"> ...

  5. struts2简单入门-参数传递的三种方式

    三种方式的简单说明 属性传递 把参数定义为属性提供get/set方法. 使用情况 参数少,不需要共享. 演示代码 public class LoginAction extends ActionSupp ...

  6. 头像截图上传三种方式之一(一个简单易用的flash插件)(asp.net版本)

    flash中有版权声明,不适合商业开发.这是官网地址:http://www.hdfu.net/ 本文参考了http://blog.csdn.net/yafei450225664/article/det ...

  7. (转)maven怎么 引入(或引用/使用) 自定义(或本地/第三方) jar的三种方式 图文教程 方法二最简单

    转:https://blog.csdn.net/wabiaozia/article/details/52798194 准备工作: 假如我有一个自定义jar是:123456.jar,下载地址http:/ ...

  8. 示例讲解PostgreSQL表分区的三种方式

    我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 简介 表分区是解决一些因单表过大引用的性能问题的方式,比如某张表过大就会造成查询变慢,可能分区是一种解决方案.一般建议 ...

  9. 【百度地图API】关于如何进行城市切换的三种方式

    原文:[百度地图API]关于如何进行城市切换的三种方式 摘要:本文介绍了三种切换城市的方式:查询城市.城市列表和显示城市轮廓. ------------------------------------ ...

随机推荐

  1. 墙国内新建Rails应用的要点(windows 7环境, Rails 4.2.0)

    1. 使用rails new 命令创建完的应用在自动执行bundle install不会成功,根据出错提示,判断原因有可能是被墙与https的证书的安全性问题. 作为开发环境,选用绕开的办法,在目录  ...

  2. python 控制 cmd 命令行颜色

    基于win7 + python3.4 import ctypes import sys '''Windows CMD命令行颜色''' # 句柄号 STD_INPUT_HANDLE = -10 STD_ ...

  3. C++系列: 如何将十六机制的字符串转成整数

    bool convertHexStringToInt(char* pstrHex, unsigned long* pResult) { ) return false; else return true ...

  4. Audrion小车实验过程

    Audrion小车实验过程 一.实验过程 拷贝光盘文件,安装驱动及Arduino软件,观看了教学视频,明白了软件操作界面的各类按钮的含义,进行了事例的上传实验. 接下来就进行了小车的安装工作,这部分不 ...

  5. Html中metra的含义

    from: http://dev.csdn.net/article/60/60902.shtm meta是用来在HTML文档中模拟HTTP协议的响应头报文.meta 标签用于网页的<head&g ...

  6. 免费的SSL证书,免费为微信小程序搭建https

    StartSSL 也免费提供了一个证书(纯英文) 申请地址:https://www.startssl.com/Account?r=L1ZhbGlkYXRl 阿里云免费提供的证书 https://com ...

  7. 解决SourceGrid在某些系统上无法用鼠标滚轮滚动的问题

    4.40版源码,找到SourceGrid/SourceGrid/Common/CustomScrollControl.cs 这个文件 定位 CustomScrollWheel() 方法,把if条件判断 ...

  8. Newtonsoft.Json之JArray, JObject, JPropertyJValue

    JObject staff = new JObject(); staff.Add(new JProperty("Name", "Jack")); staff.A ...

  9. [BZOJ 2241][SDOI2011]打地鼠(枚举+预处理)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2241 分析: 鉴于R,C的取值很小,于是可以人为枚举R和C的大小,然后判定这个规格的锤 ...

  10. c#简单自定义异常处理日志辅助类

    简单写了一个错误日志记录辅助类,记录在此. Loghelper类 using System; using System.Collections.Generic; using System.IO; us ...