Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Send parameter using XMLHttpRequest

 
Steve Dyke
Ranch Hand
Posts: 1642
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to use XMLHttpRequest to access a servlet. I would like to send it a value and based on the value return a specific value derived from a java class. So far all I can do is access the servlet.

My JSP code is:

[/CODE]
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Send uses name/value pairs. But if you are using a GET, you need to append the value as a querystring parameter in the URL that you are posting to.


Eric
 
Steve Dyke
Ranch Hand
Posts: 1642
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have changed my JSP code to:

 
Steve Dyke
Ranch Hand
Posts: 1642
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have got the parameter passing from my JSP javascript to a servlet. I have my servlet creating a new connection object and returning a desired value from the SQL result.

My sistuation is now how to return this value from the server back to the calling JSP and populate a field on the form.

My servlet code:

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly what are you trying to return? Not to pick on you, but your posts have a history of asking "How do I do this?" without explaining exactly what this is.

You leave your servlet code dangling with assignments to MiscObjects instances, yet no one but you knows what MiscObjects is, or what you want to do with it. Or them, there are two. Which contains the useful data?

Such details are necessary for any substantive response to your questions.

Please read this.
[ May 22, 2007: Message edited by: Bear Bibeault ]
 
Steve Dyke
Ranch Hand
Posts: 1642
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope this gives the detail you need to help me. I will try and do better with my posts. Thanks for all your help.

I have a JSP in which the user fills out a form. On eof the fields is a customer job number. When the user types in this number I want another field on the form to be auto populated with a cooresponding CustomerID.

The job numbers and customerid's are located on an AS400 database.

Right now I have javascript in my JSP using the XMLHttpRequest object calling a servlet(url with a job number as a parameter) . The servlet creates an instance of a java connection class which returns the customerID from the result set. I can verify this by a System.out.println(). This is a string value.

My problem is how do I get this string back to the calling JSP?

I do not understand how to manipulate or control the response of the servlet. Do I some how give the response the value of the customerID string.

My last post contains my servlet code.
 
Steve Dyke
Ranch Hand
Posts: 1642
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I posted my lst message the last post by Bear was apperantly not complete yet beacuse I did not know I needs to address the MiscObjects. Yes my Servlet code is left dangleing because I do not know what to do next. I have assigned the result of the connection result to the MiscObjects jobCustomerString to make sure it is a string. The MiscObjects class has a getMiscString(). It is this value I need to send back to the calling JSP field. Please gnore the other MiscObjects code.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To write the response:

response.getWriter().print( someString );

This is a very basic servlet concept. If you are going to be working with servlets, I'd suggest grabbing a good book or finding a good tutorial.

This response text can be read back from the XHR instance once the request is completed.
 
Steve Dyke
Ranch Hand
Posts: 1642
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We are really getting close to a soultion.

My servlet has the following:



My JSP javascript has:



the alert gives me <Profile>BEJ </Profile>

How do I get the BEJ by itself so I can use it as a value.

Thanks again.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
arg1.getWriter().print(jobNumberString.getMiscString());

alert(req.responseText);

You don't have to return XML if you don't want to. In fact, I rarely do.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
XML is the pits in JavaScript. If all you need is a string, just return the string. If you want to return something structured, use JSON.
 
Steve Dyke
Ranch Hand
Posts: 1642
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. This works great.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic