• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

getParameter in servlet from Ajax call returns null  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I have a name and pw text fields with a submit button. The problem is that whatever value I enter does not get picked up by the servlet. I do not know if the problem is when JQUERY is trying to get the HTML element value or if the servlet cannot get the values from the ajax call.

I have a servlet which prints out the request.getParameters and it keeps printing null. I have attached relevant snippets of the html and js code and the servlet code to test if the parameters are being received.

HTML code:




JavaScript code (done in a document.ready function):



And finally the servlet code to test if the values are being sent over:



I keep getting 3 null values. It must be a problem between ajax and servlet. If you look at the method attribute, its not being taken from user input or any html element it is hard coded but even that returns null in the servlet.
 
Sheriff
Posts: 21689
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're sending the data as JSON but are attempting to read it as if you've sent them as a form. Either sent a form POST (I even think that's the default for $.ajax), or change your servlet to read the request's input stream and parse it to JSON. You can use Jackson's ObjectMapper for that.
 
Hussain Faruk
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:You're sending the data as JSON but are attempting to read it as if you've sent them as a form. Either sent a form POST (I even think that's the default for $.ajax), or change your servlet to read the request's input stream and parse it to JSON. You can use Jackson's ObjectMapper for that.


Thank you so much. Few questions:
1) Does doPost only work on HTML FORM elements? So that would mean I have to change my html to include a form rather than just input text for the username and password fields.
2) The request input stream is already in Json. The javascript does the jsonstringify to the data so why would I need to covert the request data into json if it is already in that format?
3) Finally, is there an equivalent ObjectMapper for GSON instead of Jackson as that is what I am using.
Many thanks
 
Rob Spoor
Sheriff
Posts: 21689
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) doPost works for any request with the HTTP POST method, whether the request body is form data, JSON, XML or even binary content. It's getParameter that's limited to both form posts (HTML FORM) and query parameters.
2) The browser code indeed sends the data to the server as JSON. The server must now learn to use (read: parse) that JSON. (I should have said "from JSON", not "to JSON".)
3) You can use GSON with its Gson objects to do mapping as well, not a problem.
 
Hussain Faruk
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:1) doPost works for any request with the HTTP POST method, whether the request body is form data, JSON, XML or even binary content. It's getParameter that's limited to both form posts (HTML FORM) and query parameters.
2) The browser code indeed sends the data to the server as JSON. The server must now learn to use (read: parse) that JSON. (I should have said "from JSON", not "to JSON".)
3) You can use GSON with its Gson objects to do mapping as well, not a problem.


Thank you. Rather than using an object mapper wouldn’t it be more simple to send my data as a format the the servlet can automatically read? This would be more efficient as the data wouldn’t need to be parsed and converted. If so, how would I implement this?
 
Rob Spoor
Sheriff
Posts: 21689
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just change your AJAX request to send a form-post instead of JSON.

According to https://devdocs.io/jquery/jquery.ajax (my #1 source for any JavaScript / jQuery related questions), the data will be a query string. Those work both for query parameters, but also for form posts.

Based on the first example (although you should prefer "method" over "type"):
 
What are you doing? You are supposed to be reading this tiny ad!
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!