HttpServletRequest获取请求参数
Posted jakin3130
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HttpServletRequest获取请求参数相关的知识,希望对你有一定的参考价值。
通过HttpServletRequest接收请求来的参数,
get请求
public class controller1 {
@RequestMapping(value = "/", method = {RequestMethod.POST})
public String con001(HttpServletRequest request) throws IOException {
Map<String, String> param = getAllRequestParam(request);
String queryString = request.getQueryString();
String user = request.getParameter("user");
String pass = request.getParameter("pass");
System.out.println(user);
System.out.println(param);
return "hello";
}
post请求
先获取到参数名的集合,在组装到Map里
private Map<String, String> getAllRequestParam(final HttpServletRequest request) {
Map<String, String> res = new HashMap<String, String>();
Enumeration<?> temp = request.getParameterNames();
if (null != temp) {
while (temp.hasMoreElements()) {
String en = (String) temp.nextElement();
String value = request.getParameter(en);
res.put(en, value);
}
}
return res;
}
以上是关于HttpServletRequest获取请求参数的主要内容,如果未能解决你的问题,请参考以下文章
Java拦截器HandlerInterceptor重写preHandle方法时HttpServletRequest无法获取自定义请求头参数问题
Java拦截器HandlerInterceptor重写preHandle方法时HttpServletRequest无法获取自定义请求头参数问题
获取HttpServletRequest请求信息,HTTP协议信息