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(
     '' + 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?
Who is Participating?
kawasConnect With a Mentor Commented:
var theURL = your_url_to_servlet_with_parameters;
var request = getXMLhttp(); // assuming that this gets the XMLHttpRequest object for you"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()
in doGet

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

1. See above
2. See above
tmonteitAuthor Commented:

I want to capture the output in a JS var.

What does the JS Code look like that captures that?
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

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

how do I integrate it into my existing js function?
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.