Solved

Java Servlet to AJAX return value

Posted on 2006-11-15
6
669 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 500 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
Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

 
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

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

734 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