Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

can a servlet do POST or what?

 
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to clarify, is this what you did?
JSP


ContactUs Servlet
 
Vinnie Jenks
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Doe, a deer, a female deer. Ray, a pockeful of sun. Me, a name, I call my tiny ad ...
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic