HttpServletRequest如何获取POST请求参数呢?

java-教程王 Servlet 发布时间:2022-01-01 21:35:37 阅读数:3557 1
下文讲述HttpServletRequest获取POST请求参数的方法分享,如下所示:
例:

方式1:request.getInputStream()

private static String getPostData(HttpServletRequest request) {
	StringBuffer data = new StringBuffer();
	String line = null;
	BufferedReader reader = null;
	try {
		reader = request.getReader();
		while (null != (line = reader.readLine()))
			data.append(line);
	} catch (IOException e) {
	} finally {
	}
	return data.toString();
}

使用注解的方式获取参数值

@RequestBody
@RequestMapping(value = "/testurl", method = RequestMethod.POST)
@ResponseBody
public ServiceResult TestUrl(HttpServletRequest request,
	@RequestBody JSONObject jsonObject){
   String username =  jsonObject.get("username").toString();
   String pwd = jsonObject.get("pwd").toString();
}
@RequestBody 可以使用JSONObject, Map ,或者ObjectDTO绑定body。 

@RequestParam
@RequestMapping(value = "/testurl", method = RequestMethod.POST)
@ResponseBody
public ServiceResult TestUrl(HttpServletRequest request,@RequestParam("username")String username,
@RequestParam("pwd")String pwd)  {
  String txt = username + pwd;
}
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/Servlet/202201/242.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者