【发布时间】:2021-11-04 02:44:24
【问题描述】:
我有一个 Java servlet,当我向它发送一个 POST 请求时,一切正常。问题是,如果发送 GET 请求,我想执行与 POST 请求相同的过程,当我运行 request.getParameterMap() 时 size = 0。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
LinkedList<String> lErr = new LinkedList<String>();
System.out.printf("Request size: %d%n", request.getContentLength());
System.out.printf("Request method: %s%n", request.getMethod());
Map map = request.getParameterMap();
System.out.printf("ParameterMap size: %d%n", map.size());
}
可能是什么问题?
POST 和 GET 请求的正文中都有数据。我使用表单数据发送它,因为 getParameterMap() 不仅支持查询字符串,而且还支持。 POST 请求工作正常。
【问题讨论】:
-
根据您提供的信息,我会说您没有在 URL 查询字符串中提供任何参数。
-
“问题是,如果发送一个 GET 请求,我想做与 POST 请求相同的过程”。这是根本错误的。
-
GET 请求没有正文。参数来自查询字符串
-
GET 请求中的参数来自 URL 查询部分,而不是来自请求的正文,将被忽略。
标签: java spring servlets request