1.通过ServletActionContext类

//获取request对象
HttpServletRequest request = ServletActionContext.getRequest();
//保存值到application中
ServletActionContext.getActionContext(request).setApplication(new HashMap<>());

2.通过ServletRequestAware 接口

    public class UserAction extends ActionSupport implements ServletRequestAware {

    private String username;

    private String userpwd;

    private HttpServletRequest request;

    public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getUserpwd() {
return userpwd;
} public void setUserpwd(String userpwd) {
this.userpwd = userpwd;
} @Override
public void setServletRequest(HttpServletRequest arg0) { this.request = arg0;
} public void executeAjax() throws IOException { // 处理Ajax请求 String name = request.getParameter("username"); String pwd = request.getParameter("userpwd"); HttpServletResponse response = ServletActionContext.getResponse(); response.getWriter().print("hello"+username+"hello"+userpwd); } }

Struts2之获取ServletAPI的更多相关文章

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

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

  2. struts2学习笔记--总结获取servletAPI的几种方式

    struts2的Action放弃了request,response等ServletAPI,使得在业务层上更加独立,在有时候使用struts2进行Web开发的时候,不可避免的要在action中使用ser ...

  3. struts2中获取request、response,与android客户端进行交互(文件传递给客户端)

    用struts2作为服务器框架,与android客户端进行交互需要得到request.response对象. struts2中获取request.response有两种方法. 第一种:利用Servle ...

  4. Struts2中获取HttpServletRequest,HttpSession等的几种方式

    转自:http://www.kaifajie.cn/struts/8944.html package com.log; import java.io.IOException; import java. ...

  5. Struts2 后台获取路径的几种方法

    Struts2 后台获取路径的几种方法 package actions.app; import java.io.File; import org.apache.struts2.ServletActio ...

  6. struts2框架学习笔记3:获取servletAPI

    Struts2存在一个对象ActionContext(本质是Map),可以获得原生的request,response,ServletContext 还可以获得四大域对象(Map),以及param参数( ...

  7. Struts2笔记——与ServletAPI解耦

    与ServletAPI解耦的访问方式 为了避免与 Servlet API 耦合在一起, 方便 Action 做单元测试, Struts2 对 HttpServletRequest, HttpSessi ...

  8. Action中获取servletAPI对象的方法

    1.ServletActionContext:可以从中获取当前Action对象需要的一切ServletAPI的相关对象: 常用的方法: 1.获取HttpServletRequest:ServletAc ...

  9. Struts2中获取servlet API的几种方式

    struts2是一个全新的MVC框架,如今被广大的企业和开发者所使用,它的功能非常强大.这给我们在使用servlet 纯java代码写项目的时候带来了福音.但是一般来说,我们的项目不到一定规模并不需要 ...

随机推荐

  1. 安装FaaS

    [root@localhost ~]# [root@localhost ~]# new OS:centos-7 [root@localhost ~]# [root@localhost ~]# vim ...

  2. [IM002] [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序

    Problems meet in the project: [IM002] [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序((IM002) [Microso ...

  3. 610K图纸打印新版增值税发票不完整的调整方法 黑盘红盘都兼容

    新版增票页面设置增票向下0.8向右-10,5刻度进纸测试 向右调整可能会有些出入 根据情况微调即可. 下面为黑盘的设置 可与上面兼容

  4. Linux下git安装配置

    一.Linux下git安装配置 2013-07-28 20:32:10|  分类: 默认分类 |  标签:linux  git  server  |举报|字号 订阅     http://abomby ...

  5. Windows里服务中没有 MySQL

    1.以管理员身份启动命令提示符 2.安装并启动MySQL服务 C:\Windows\system32>mysqld.exe -installService successfully instal ...

  6. 【leetcode】1038. Binary Search Tree to Greater Sum Tree

    题目如下: Given the root of a binary search tree with distinct values, modify it so that every node has ...

  7. Flink 实战:如何解决生产环境中的技术难题?

    大数据作为未来技术的基石已成为国家基础性战略资源,挖掘数据无穷潜力,将算力推至极致是整个社会面临的挑战与难题. Apache Flink 作为业界公认为最好的流计算引擎,不仅仅局限于做流处理,而是一套 ...

  8. [CSP-S模拟测试]:折纸(模拟)

    题目描述 小$s$很喜欢折纸.有一天,他得到了一条很长的纸带,他把它从左向右均匀划分为$N$个单位长度,并且在每份的边界处分别标上数字$0\sim n$.然后小$s$开始无聊的折纸,每次他都会选择一个 ...

  9. HDU 6024 Building Shops (简单dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6024 题意:有n个room在一条直线上,需要这这些room里面建造商店,如果第i个room建造,则要总 ...

  10. Jmeter中if 控制器的使用

    使用if控制器有两种方式:1.不勾选“interpret condition as variable expression”直接输入我们需要判断的表达式即可,判断表达式为真时,执行if控制器下的请求, ...