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?
tmonteitAsked:
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
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
 
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
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
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
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.