Issue
I have a Java servlet and when I send it a POST request everything works just fine. The thing is that if send a GET request i want to make the same procedure as if it is a POST req, when I run request.getParameterMap() has 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());
}
What could the problem be?
Both POST and GET request have data in their body. I am sending it using form-data as getParameterMap() does not only supports query string but that also. POST request works just fine.
Solution
As Joao and some more people pointed out, the way I was told to do this stuff was wrong. GET request get their body ignored therefore parametermap values come from the querystring not the body. So if nothing goes via querystring but in form-data in a GET request, parametermap size will be 0. Thank you guys!
The thing will to start using the doGet()
method properly and not just passing that parameters to doPost()
.
I was so focused on the task that i couldn't even think properly.
Answered By - Carlos Martel Lamas
Answer Checked By - David Marino (JavaFixing Volunteer)