• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 678
  • Last Modified:

Java Servlet to AJAX return value

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
tmonteit
Asked:
tmonteit
  • 3
  • 2
1 Solution
 
CEHJCommented:
in doGet

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

1. See above
2. See above
0
 
tmonteitAuthor Commented:

I want to capture the output in a JS var.

What does the JS Code look like that captures that?
0
 
kawasCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
kawasCommented:
should read:
if (request.readyState == 4 && request.status == 200) {
          // s contains the number
          var s = request.responseText;
}
0
 
tmonteitAuthor Commented:
I'm using the 'prototype' js library does that make a difference?

how do I integrate it into my existing js function?
0
 
kawasCommented:
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now