Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Servlets Getting POST Parameters???

saw7988saw7988 Posts: 14Member
Hi,

I'm writing a servlet that handles the action from a form. When the form uses the "GET" HTTP method and I do request.getParameter(whatever_parameter) it works just like it should. However, when I change the method of the form to "POST" the getParameter function always returns null. I'm pretty sure my form is not the problem, and neither is my servlet code. I've read in many many places that the request.getParameter function works with both methods. Is this true? If so what am I doing wrong?

If this technique is incorrect for POST, is there a way to directly read the parameters using some kind of InputStream technique?

Thanks

Comments

  • saw7988saw7988 Posts: 14Member
    A followup to my previous post...

    I added in a toString after the getParameter function in doPost...

    request.getParameter("param") changed to request.getParameter("param").toString()

    This seemed to fix the problem, although I don't know why. I'm testing using System.out.println's and I was under the impression that it used the toString method on all objects anyway.

    Could this be due to a difference between GET and POST in the way the parameters are sent?
  • saw7988saw7988 Posts: 14Member
    : A followup to my previous post...
    :
    : I added in a toString after the getParameter function in doPost...
    :
    : request.getParameter("param") changed to
    : request.getParameter("param").toString()
    :
    : This seemed to fix the problem, although I don't know why. I'm
    : testing using System.out.println's and I was under the impression
    : that it used the toString method on all objects anyway.
    :
    : Could this be due to a difference between GET and POST in the way
    : the parameters are sent?

    Sorry, my bad. Disregard this follow-up post. That only worked once (I don't know why) and since the getParameter is always returning null, doing a toString just causes a NullPointerException because it dereferences the null String.

    The question still remains...

    How do I get the parameters from a POST request and why is getParameter not working?
  • zibadianzibadian Posts: 6,349Member
    : : A followup to my previous post...
    : :
    : : I added in a toString after the getParameter function in doPost...
    : :
    : : request.getParameter("param") changed to
    : : request.getParameter("param").toString()
    : :
    : : This seemed to fix the problem, although I don't know why. I'm
    : : testing using System.out.println's and I was under the impression
    : : that it used the toString method on all objects anyway.
    : :
    : : Could this be due to a difference between GET and POST in the way
    : : the parameters are sent?
    :
    : Sorry, my bad. Disregard this follow-up post. That only worked once
    : (I don't know why) and since the getParameter is always returning
    : null, doing a toString just causes a NullPointerException because it
    : dereferences the null String.
    :
    : The question still remains...
    :
    : How do I get the parameters from a POST request and why is
    : getParameter not working?

    POST doesn't use the URL parameter to send the data, but the request body. Only the GET request uses the parameter for additional data. To get the data from a POST try using the getReader().
  • gnv_ravindragnv_ravindra Posts: 46Member
    : : : A followup to my previous post...
    : : :
    : : : I added in a toString after the getParameter function in doPost...
    : : :
    : : : request.getParameter("param") changed to
    : : : request.getParameter("param").toString()
    : : :
    : : : This seemed to fix the problem, although I don't know why. I'm
    : : : testing using System.out.println's and I was under the impression
    : : : that it used the toString method on all objects anyway.
    : : :
    : : : Could this be due to a difference between GET and POST in the way
    : : : the parameters are sent?
    : :
    : : Sorry, my bad. Disregard this follow-up post. That only worked once
    : : (I don't know why) and since the getParameter is always returning
    : : null, doing a toString just causes a NullPointerException because it
    : : dereferences the null String.
    : :
    : : The question still remains...
    : :
    : : How do I get the parameters from a POST request and why is
    : : getParameter not working?
    :
    : POST doesn't use the URL parameter to send the data, but the request
    : body. Only the GET request uses the parameter for additional data.
    : To get the data from a POST try using the getReader().

    request.getParameter() will retrieve parameters either by GET or POST methods. You should be sure that the spelling & case (upper / lower) of parameter name used in the form and in the servlet are same.
  • saw7988saw7988 Posts: 14Member
    : : : : A followup to my previous post...
    : : : :
    : : : : I added in a toString after the getParameter function in doPost...
    : : : :
    : : : : request.getParameter("param") changed to
    : : : : request.getParameter("param").toString()
    : : : :
    : : : : This seemed to fix the problem, although I don't know why. I'm
    : : : : testing using System.out.println's and I was under the impression
    : : : : that it used the toString method on all objects anyway.
    : : : :
    : : : : Could this be due to a difference between GET and POST in the way
    : : : : the parameters are sent?
    : : :
    : : : Sorry, my bad. Disregard this follow-up post. That only worked once
    : : : (I don't know why) and since the getParameter is always returning
    : : : null, doing a toString just causes a NullPointerException because it
    : : : dereferences the null String.
    : : :
    : : : The question still remains...
    : : :
    : : : How do I get the parameters from a POST request and why is
    : : : getParameter not working?
    : :
    : : POST doesn't use the URL parameter to send the data, but the request
    : : body. Only the GET request uses the parameter for additional data.
    : : To get the data from a POST try using the getReader().
    :
    : request.getParameter() will retrieve parameters either by GET or
    : POST methods. You should be sure that the spelling & case (upper /
    : lower) of parameter name used in the form and in the servlet are
    : same.

    Well, I figured it all out. The form that was sent to this page has an enctype of "multipart/form-data" because it involves uploading a file. With this encryption type, the getParameter function of the request object cannot be used. In order to retrieve the parameters a MultipartRequest object can be created and used. I don't know if this is a publicly available class or not, but I used the one made by another engineer at my company.
  • gnv_ravindragnv_ravindra Posts: 46Member
    Can anybody tell me what is the maximum size allowed in file upload?


  • ndifrekendifreke Posts: 49Member
    : : : : : A followup to my previous post...
    : : : : :
    : : : : : I added in a toString after the getParameter function in doPost...
    : : : : :
    : : : : : request.getParameter("param") changed to
    : : : : : request.getParameter("param").toString()
    : : : : :
    : : : : : This seemed to fix the problem, although I don't know why. I'm
    : : : : : testing using System.out.println's and I was under the impression
    : : : : : that it used the toString method on all objects anyway.
    : : : : :
    : : : : : Could this be due to a difference between GET and POST in the way
    : : : : : the parameters are sent?
    : : : :
    : : : : Sorry, my bad. Disregard this follow-up post. That only worked once
    : : : : (I don't know why) and since the getParameter is always returning
    : : : : null, doing a toString just causes a NullPointerException because it
    : : : : dereferences the null String.
    : : : :
    : : : : The question still remains...
    : : : :
    : : : : How do I get the parameters from a POST request and why is
    : : : : getParameter not working?
    : : :
    : : : POST doesn't use the URL parameter to send the data, but the request
    : : : body. Only the GET request uses the parameter for additional data.
    : : : To get the data from a POST try using the getReader().
    : :
    : : request.getParameter() will retrieve parameters either by GET or
    : : POST methods. You should be sure that the spelling & case (upper /
    : : lower) of parameter name used in the form and in the servlet are
    : : same.
    :
    : Well, I figured it all out. The form that was sent to this page has
    : an enctype of "multipart/form-data" because it involves uploading a
    : file. With this encryption type, the getParameter function of the
    : request object cannot be used. In order to retrieve the parameters a
    : MultipartRequest object can be created and used. I don't know if
    : this is a publicly available class or not, but I used the one made
    : by another engineer at my company.


    there is a time i worked with servlets with alot of forms and i ran into ur post problem. i solved the problem by turning to jsp. well jsp is better for stuffs like that. use ur servlets to redirect controls.else u may end up wtiring long codes in servlets that do nothing.
Sign In or Register to comment.