-2

I'was debugging a client-side AJAX problem that is making request to servlet. But the bug turned out to be at server side. You can refer to my original question here. From the discussion with more experienced people, I found out that servlet is using request.getAttribute() method to retrieve parameters from the request instead of getParameter(). So I thought to open a new question to clear my doubt.

Now my question is: If I use GET method to pass parameters from client to server, getAttribute() in Servlet works fine and I can get param values. But when I use POST method, getAttribute() returns null. Why does it work for GET and not for POST?

Community
  • 1
  • 1
indusBull
  • 1,834
  • 5
  • 27
  • 39

2 Answers2

3

You should always use getParameter, when attribute come from GET or POST method. And use getAttribute when request is forwarded from another servlet/jsp. Such as :

ServletA:
request.setAttribute("test","value")
request.getRequestDispatcher("/ServletB").forward(request, response)


ServletB:
request.getAttribute("test") <-- you can get test attribute by using getAttribute
NoodleX
  • 709
  • 1
  • 7
  • 21
  • I got that point after some research. But my question is why can I retrieve parameter values sent with GET method using getAttribute() but not from POST method. – indusBull Sep 01 '11 at 05:43
  • 1
    as far as i know it's not possible to get variable which submitted by your HTML form (either GET or POST) by using getAttribute. Attribute might be set previously by servlet. – NoodleX Sep 01 '11 at 05:48
  • 2
    If, indeed, you are able to extract request parameters using getAttribute(), then that's a non-standard bug/feature in the particular servlet container implementation you are using. It's definitly not the way it's supposed to work, according the the servlet API specification. – pap Sep 01 '11 at 07:20
0

Now my question is: If I use GET method to pass parameters from client to server, getAttribute() in Servlet works fine and I can get param values. But when I use POST method, getAttribute() returns null. Why does it work for GET and not for POST?

Complete nonsense. You're apparently working on an existing project which has a lot of other existing servlets and filters. My cents that there's another filter in the request-response chain which maps request parameters to request attribtues for some unobvious reason.

Please create a blank playground project and create a playground servlet to familarize yourself better with servlets without all that noise in existing projects.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • "another filter" - that's what I was looking for. I've already mentioned that Servlet is written by someone else. After closer look to code, I've found out that the programmer was setting all request parameters to attributes when doGet() or doPost() are called and then accessing parameters from getAttribute(). It is "Complete Nonesense" for someone who has clear understanding. I am newbie, so naturally "nonsense" question comes to mind sometimes. – indusBull Sep 01 '11 at 18:03