aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Send parameter using XMLHttpRequest Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Send parameter using XMLHttpRequest" Watch "Send parameter using XMLHttpRequest" New topic
Author

Send parameter using XMLHttpRequest

Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1475
    
    1
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Nov 16, 2004
Posts: 1475
    
    1
I have changed my JSP code to:

Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1475
    
    1
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

Joined: Jan 10, 2002
Posts: 61592
    
  67

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 ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1475
    
    1
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

Joined: Nov 16, 2004
Posts: 1475
    
    1
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

Joined: Jan 10, 2002
Posts: 61592
    
  67

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

Joined: Nov 16, 2004
Posts: 1475
    
    1
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
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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.


GenRocket - Experts at Building Test Data
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61592
    
  67

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

Joined: Nov 16, 2004
Posts: 1475
    
    1
Thanks. This works great.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Send parameter using XMLHttpRequest