This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Servlets and the fly likes can a servlet do POST or what? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "can a servlet do POST or what?" Watch "can a servlet do POST or what?" New topic
Author

can a servlet do POST or what?

Vinnie Jenks
Ranch Hand

Joined: Apr 26, 2004
Posts: 207
What you'd *think* would be easy to find on google, sometimes just isn't.

I simply need to know if I can somehow retrieve POST form values (in a servlet) that were sent by a JSP.

JSP POST Form -> Servlet (display values submitted) -> redirect to another page.

I can't figure out how to get the servlet to understand POST values that the form sent it...the value is always 'null'.

I understand doGet and doPost and I'm following every tutorial I can find but it isn't working.

Here is the form value in "form.jsp":



...this form submits to ContactUs, which is a servlet:

James Lye
Greenhorn

Joined: Nov 12, 2004
Posts: 6
I think you should cut the codes in the doGet method and paste them in the doPost.
Also, in your JSP, make sure you have:
Vinnie Jenks
Ranch Hand

Joined: Apr 26, 2004
Posts: 207
I've tried that and the form is setup exactly like that, it is still null output upon posting the form. I didn't post the whole form so as to keep it short and sweet.

However, the page/servlet the form resides in is actually a jsp that has had a request forwarded to it from another servlet using a RequestDispatcher..this servlet is called Router. Would that affect how the form and POST would behave?

So the layout is like this:

Router servlet --forward request--> form.jsp --post--> ContactUs servlet.

I want to use post as it is more secure and wouldn't require form values to be globbed onto the end of the URL in a querystring. There is no judging how long the string may be due to user-input as well.
James Lye
Greenhorn

Joined: Nov 12, 2004
Posts: 6
Just to clarify, is this what you did?
JSP


ContactUs Servlet
Vinnie Jenks
Ranch Hand

Joined: Apr 26, 2004
Posts: 207
Yes, that is what I did.

However, the form is in a JSP that is "controlled" by a servlet, using the RequestDispatcher MVC approach.

Not sure if that somehow affects the way doPost is handled...I wouldn't think so?

Otherwise, yes, that is exactly what I've done and the "name" variable always returns null.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12792
    
    5
If this was my problem the first thing I would do is "view source" the html page that the browser actually gets. There are lots of errors you can detect that way - for example, the input element may not actually be inside the form, or the spelling of the name value may be different from what you expect.
Bill
Mark Stein
Ranch Hand

Joined: May 20, 2002
Posts: 75
a servlet can do either doPost or doGet. However, when doing doPost, parameters can only be read once, so make sure you assign them to variables in the servlet the first time you access them.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12792
    
    5
a servlet can do either doPost or doGet. However, when doing doPost, parameters can only be read once, so make sure you assign them to variables in the servlet the first time you access them.

That is simply not true. See the source code in the
org.apache.catalina.connector.HttpRequestBase.java file for the method
protected void parseParameters()

Where you can see that once this has run, the parsed parameter map is stored for reuse.

There is an interaction between using getInputStream and getParameter - perhaps that is the cause of your confusion.
Bill
Vinnie Jenks
Ranch Hand

Joined: Apr 26, 2004
Posts: 207
The solution was more vague than that and I don't understand the inner-workings of how servlets handle post/get requests to know *why* this worked; I simply added a name attribute to the form tag (i.e. name="formname") and it works.

Ha!

Thanks everyone!
 
GeeCON Prague 2014
 
subject: can a servlet do POST or what?