Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to read POST type parameters in a servlet using doPOST

 
Kenneth Owino
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi every one. i need some help on how to read POST request parameters in a servlet. i tried getParameter() but didn't work n got an error that "NOT FOUND"; i read on internet that i need to use a bufferedreader and getReader() or send the parameters as headers from my midlet and then use getHeader() but have still failed to find a clear example on how i can read individual parameters assigning them to given variables like it is with doGet(). getHeader too is not clear in the sites i have looked at. please help with some explanation and code on how i can recieve POST parameters in my servlet using either getReader or getheader or even using both. Thanks alot
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to the Servlets forum.

Please be more descriptive in describing what went wrong. request.getParameter() works fine for POSTs. Are you using a multipart form, perhaps? There is no way a getParameter() returned anything like "NOT FOUND" unless that was the actual data; if a parameter wasn't found it would return null.
 
Kenneth Owino
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i actually get a reponse "BAD REQUEST". "NOT FOUND" was a different case that i resolved, pardon me. but the problem remains that the POST is not working
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to TellTheDetails, and read ItDoesntWorkIsUseless. You are giving us zero information to work with, so it's impossible to help. getParameter() works just fine with a POST request, so something else is obviously wrong.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
POST is a HTTP method name, when a client make a request to the server, server/container will check the HTTP method, and according to it, it'll invoke doPOST or other methods, if that request is to a servlet or jsp. And then container makes a HttpRequest and HttpRespone object and pass it to the methods. The POST method parameters will be attached to the HttpRequest object and passed at the corresponding methods. And have a look on APIs to know the details about these two objects and it's methods.
 
Vijitha Kumara
Bartender
Posts: 3953
21
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kenneth Owino wrote:...but have still failed to find a clear example on how i can read individual parameters assigning them to given variables like it is with doGet()...

If you know how to retrieve parameters with a GET request (as it seems like you know in your above statement) it's no different with POST except which method (i.e: doPost()) in which you do that.
 
Kenneth Owino
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys. I attached the parameters to the url in the midlet like it is done with GET and my program worked fine. My worry now is that POST is no different from GET yet I read that with POST you can send parameters as part of the request body via the output stream. Please let me know why POST is only working when I use the url with parameters attached and not when I send them in the body. Could this mean that getParameter only works when the parameters were sent as part of the url? How can I send the parameters without attaching them to the url and recieve them at the servlet? Thanks
 
Vijitha Kumara
Bartender
Posts: 3953
21
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kenneth Owino wrote:... How can I send the parameters without attaching them to the url and recieve them at the servlet?

By sending a POST request to the server..
... I read that with POST you can send parameters as part of the request body

Correct. Thats one of the key differences in GET and POST.
Please let me know why POST is only working when I use the url with parameters attached and not when I send them in the body.

There should be something wrong the way you use it.
Could this mean that getParameter only works when the parameters were sent as part of the url?

No.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why didn't you say it was a midlet? When I ask you to TellTheDetails I'm not just trying to generate traffic to our FAQ pages.

Does this help at all?

http://codetrips.blogspot.com/2007/04/http-post-from-j2me-midlet.html

Moving to the appropriate forum.
 
Tim Holloway
Bartender
Posts: 18419
60
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hate to play ping-pong with this thread, but there's really nothing JME-specific about the problem. Reading "parameters" - which I believe actually means form fields in this case - is done on the server, so I'm going to bounce this thread back to the servlets forum.

The REAL problem is that you don't understand how HTTP POST works.

When you send down a request using request-type POST, you're saying that there will be an accompanying data stream coming along with the URL and headers.

That's all you're saying.

The format of that data stream is actually completely up to you. Properly done, the data stream includes MIME-encoding information to tell the server what format that data stream has.

In real-world terms, however, web browsers will often be sending the contents of a FORM.

Here's a short reference on that: http://en.wikipedia.org/wiki/POST_(HTTP)

The actual parsing of that data stream is up to server-side code. It's too early on a Monday morning for me to remember if the Java HttpServlet class code does that for you or if you have to do it yourself. Since mostly I'm using support frameworks these days, I don't have to worry about it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!