HttpServletRequest 参数映射始终为空

HttpServletRequest parametermap is always empty

我有一个 Java servlet,当我向它发送 POST 请求时,一切正常。问题是,如果发送 GET 请求,我想执行与 POST 请求相同的过程,当我 运行 request.getParameterMap() 的大小 = 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 请求工作得很好。

正如 Joao 和其他一些人指出的那样,我被告知做这件事的方式是错误的。 GET 请求会忽略它们的正文,因此参数映射值来自查询字符串而不是正文。因此,如果没有通过查询字符串但在 GET 请求中的表单数据中,参数映射大小将为 0。谢谢你们!

事情将开始正确使用 doGet() 方法,而不仅仅是将参数传递给 doPost()

我太专注于任务,以至于我什至无法正确思考。