?
Solved

Java Servlet to AJAX return value

Posted on 2006-11-15
6
Medium Priority
?
673 Views
Last Modified: 2013-11-24
I have some JS Call that is sending 2 values to a servlet.  

 testAdd: function(a, b){
     debugAPI("testAdd: " + a + ", " + b);
     var myAjax = new Ajax.Request(
     'myServlet.do?dispatch=add&aVal=' + a + '&bVal=' + b,  
     {
        method: 'get',
        onComplete: function(response) {$F("result") = response.responseText;}
     });
   }

1.  What would the basic servlet code look like that adds these numbers?

2.  How does the servlet return the result back to the JavaScript?
0
Comment
Question by:tmonteit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17947659
in doGet

out.println(Integer.parseInt(req.getParameter("a") + Integer.parseInt(req.getParameter("b"));

1. See above
2. See above
0
 

Author Comment

by:tmonteit
ID: 17947974

I want to capture the output in a JS var.

What does the JS Code look like that captures that?
0
 
LVL 13

Accepted Solution

by:
kawas earned 2000 total points
ID: 17948090
var theURL = your_url_to_servlet_with_parameters;
var request = getXMLhttp(); // assuming that this gets the XMLHttpRequest object for you
request.open("GET", theURL, true);
request.onreadystatechange = function() {
    if (mc.request.readyState == 4 && mc.request.status == 200) {
          // s contains the number
          var s = mc.request.responseText;
    }
}; // end function()
request.send(null);
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:kawas
ID: 17948095
should read:
if (request.readyState == 4 && request.status == 200) {
          // s contains the number
          var s = request.responseText;
}
0
 

Author Comment

by:tmonteit
ID: 17948180
I'm using the 'prototype' js library does that make a difference?

how do I integrate it into my existing js function?
0
 
LVL 13

Expert Comment

by:kawas
ID: 17948213
It does of course. you will have to see how they use ajax. most likely, its just a method call and you wont have to use the code that i wrote. The code I wrote assumed that you were writing your own javascript methods.
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month12 days, 1 hour left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question