在servlet、filter等中获取POST请求的参数

  1. form表单形式提交post方式,可以直接从 request 的 getParameterMap 方法中获取到参数
  2. JSON形式提交post方式,则必须从 request 的 输入流 中解析获取参数,使用apache commons io 解析

maven配置

      <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.50</version>
</dependency>

获取POST请求中的参数

    /**
* @author tianwyam
* @description 从POST请求中获取参数
* @param request
* @return
* @throws Exception
*/
public static Map<String, Object> getParam4Post(HttpServletRequest request) throws Exception {

     // 返回参数
Map<String, Object> params = new HashMap<>(); // 获取内容格式
String contentType = request.getContentType();
if (contentType != null && !contentType.equals("")) {
contentType = contentType.split(";")[0];
} // form表单格式 表单形式可以从 ParameterMap中获取
if ("appliction/x-www-form-urlencoded".equalsIgnoreCase(contentType)) {
// 获取参数
Map<String, String[]> parameterMap = request.getParameterMap();
if (parameterMap != null) {
for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
params.put(entry.getKey(), entry.getValue()[0]);
}
}
} // json格式 json格式需要从request的输入流中解析获取
if ("application/json".equalsIgnoreCase(contentType)) {
// 使用 commons-io中 IOUtils 类快速获取输入流内容
String paramJson = IOUtils.toString(request.getInputStream(), "UTF-8");
Map parseObject = JSON.parseObject(paramJson, Map.class);
params.putAll(parseObject);
} return params ;
}

Servlet中获取POST请求的参数的更多相关文章

  1. jsp内置对象pageContext如何在Servlet中获取值

    pageContext javax.servlet.jsp.PageContext 的实例,该对象代表该JSP 页面上下文,使用该对象可以访问页面中的共享数据.常用的方法有getServletCont ...

  2. 如何在Silverlight应用程序中获取ASP.NET页面参数

    asp.net Silverlight应用程序中获取载体aspx页面参数 有时候SL应用中需要使用由aspx页面中传递过来的参数值,此时通常有两种方法获取 1. 使用InitParameters属性, ...

  3. Web版需求征集系统所得1,servlet中获取checkbox复选框的值

    servlet中获取checkbox复选框的值 </tr> <tr> <td align="right">研究类型</td> < ...

  4. 在Servlet中获取Spring注解的bean

    最近由于项目中出现了Servlet调用Spring的bean,由于整个项目中所有的bean均是注解方式完成,如@Service,@Repository,@Resource等,但是Spring的容器管理 ...

  5. web.xml中在Servlet中获取context-param和init-param内的参数

    引自:http://blog.csdn.net/yakson/article/details/9203231 web.xml里面可以定义两种参数:1.application范围内的参数,存放在serv ...

  6. servlet中获取配置文件中的参数.

    web.xml (添加init-param) <?xml version="1.0" encoding="UTF-8"?> <web-app ...

  7. Servlet中的属性(attribute)和参数(parameter)的区别

    1.引子 初学者对属性(attribute)和参数(parameter)容易搞混.没搞清他们的区别,项目中就可能出现一此莫名其妙的问题. 2.两者的区别 1) 属性(attribute) 属性是在后台 ...

  8. Servlet中获取JSP内置对象

    方便自己查询,嫌低级的勿喷.... 1.request 在servlet的doGet和doPost的参数中就有HttpServletRequest req参数,而JSP内置request对象就是Htt ...

  9. WCF和WebService中获取当前请求报文的方法

    WCF中: 1. 在hosting WCF的web.config中加入: <system.serviceModel> <serviceHostingEnvironment aspNe ...

随机推荐

  1. Windows:获取本地时间

    造冰箱的大熊猫@cnblogs 2019/6/4 #include <windows.h> int func() { SYSTEMTIME systime; GetLocalTime ( ...

  2. Intel Code Challenge Final Round D. Dense Subsequence 二分思想

    D. Dense Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  3. [Luogu] 选择客栈

    https://www.luogu.org/problemnew/show/P1311 思路就是,从1到n枚举,输入color和price的值,我们需要记录一个距离第二个客栈最近的咖啡厅价钱合理的客栈 ...

  4. DP(第一版)

    序 任何一种具有递推或者递归形式的计算过程,都叫做动态规划 如果你一开始学的时候就不会DP,那么你在考试的时候就一定不会想到用动态规划! 需要进行掌握的内容 1)DP中的基本概念 2)状态 3)转移方 ...

  5. Python基础之集合set

    集合是无序的,不重复的数据集合,它里面的元素是可哈希的(不可变类型), 但是集合本身是不可哈希的(所以集合做不了字典的键)的. 以下是集合最重要的两点: (1)去重,把一个列表变成集合,就自动去重了. ...

  6. AcWing:237. 程序自动分析(离散化 + 并查集)

    在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设x1,x2,x3,…x1,x2,x3,…代表程序中出现的变量,给定n个形如xi=xjxi=x ...

  7. python 列表切片之负数的含义代码示例

    a = list(range(10)) print(a[::]) #复制一个列表 print(a[::2]) #每隔2个取一次 print(a[::3]) #每隔3个取一次 print(a[::-1] ...

  8. substring(x)和substring(x,y)的用法

    substring(x)和substring(x,y)的用法 代码: public class textmu { /** * @param args */ public static void mai ...

  9. 单调队列优化dp(捡垃圾的机器人)

    /************************************************************************* > File Name: a.cpp > ...

  10. JVM | 分代垃圾回收策略的基本概念以及过程

    一.为什么要分代 分代的垃圾回收策略,是基于这样一个事实:不同的对象的生命周期是不一样的.因此,不同生命周期的对象可以采取不同的收集方式,以便提高回收效率. 在Java程序运行的过程中,会产生大量的对 ...