使用req.getParameter()无法获得post请求体里的参数怎么回事
时间: 2024-05-17 17:19:48 浏览: 128
在HTTP协议中,POST请求和GET请求的参数传递方式是不同的。
GET请求的参数是直接附加在URL后面,形如`http://example.com/path?param1=value1¶m2=value2`,所以可以使用request.getParameter()方法来获取参数值。
而POST请求的参数是放在请求体中的,使用request.getParameter()方法无法获取,需要通过其他方式来获取参数值。常见的方式有以下几种:
1. 使用request.getInputStream()方法来获取请求体的字节流,再将字节流转换为字符串,最后解析出参数值。
```java
InputStream inputStream = request.getInputStream();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, length);
}
String requestBody = outputStream.toString("UTF-8");
```
这种方式虽然可以获取请求体中的参数值,但是需要手动解析,比较麻烦。
2. 使用第三方库,比如Apache的HttpComponents,来处理POST请求。
```java
HttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
StringEntity entity = new StringEntity(jsonString, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
String responseBody = EntityUtils.toString(response.getEntity());
```
这种方式使用起来比较简单,但是需要引入第三方库,可能会增加工程的复杂度。
3. 使用Servlet 3.0中新增的request.getReader()方法来获取请求体的字符流,再将字符流转换为字符串,最后解析出参数值。
```java
BufferedReader reader = request.getReader();
StringBuilder stringBuilder = new StringBuilder();
char[] buffer = new char[1024];
int length;
while ((length = reader.read(buffer)) != -1) {
stringBuilder.append(buffer, 0, length);
}
String requestBody = stringBuilder.toString();
```
这种方式虽然比第一种方式要简单一些,但是需要注意的是,只能在Servlet 3.0及以上版本中使用。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.sql.SQLException; @WebServlet("/update") public class update extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=utf-8"); String id = req.getParameter("id"); String username = req.getParameter("username"); String password = req.getParameter("password"); UsersDao usersDao = new UsersDao(); User user = new User(); user.setUsername(username); user.setPassword(password); user.setId(Integer.valueOf(id)); try { if (usersDao.update(user)) { resp.getWriter().println("修改成功"+"返回主页"); } else { resp.getWriter().println("修改失败"+"返回主页"); } } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)