By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,738 Members | 1,730 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,738 IT Pros & Developers. It's quick & easy.

Send data from doPost() in servlet to AJAX

sid0404
P: 16
Hi

I need to send data from my servlet to my html(which contains AJAX), so as per the motivation of the AJAX, this should be done without my webpage reloading / refresh.

my code on the ajax side is something like this:

Expand|Select|Wrap|Line Numbers
  1. var xmlHttp = false;
  2.  
  3. function getXMLHttpRequest(val) {
  4.     try{
  5.         xmlHttp = new XMLHttpRequest();
  6.     }catch(err1){
  7.         try{
  8.             xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  9.         }
  10.         catch(err2){
  11.             try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");}
  12.             catch(err3){ request = null;}
  13.         }
  14.     }
  15.     if (xmlHttp == false) {
  16.         alert("Error creating request object !!");
  17.     }
  18.     else if(xmlHttp != false){
  19.         return xmlHttp;
  20.  
  21.  
  22.     }
  23. }
  24. var xmlhttp = new getXMLObject(); 
  25.  
  26. function ajaxFunction() {
  27.       if(xmlHttp) {
  28.        var value1 = document.getElementById("value1");
  29.        xmlHttp.open("POST","ajax_controlller",true); //getname will be the servlet name
  30.        xmlHttp.onreadystatechange  = handleServerResponse;
  31.        xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  32.        xmlHttp.send("value1=" + value1.value); //Posting txtname to Servlet
  33.       }
  34.     }
  35.  
  36. function handleServerResponse() {
  37.     if (xmlHttp.readyState == 4) {
  38.          if(xmlHttp.status == 200) {
  39. //           document.myForm.message.innerHTML=xmlHttp.responseText;
  40.  
  41.            alert(xmlHttp.responseText); //Update the HTML Form element
  42.          }
  43.          else {
  44.             alert("Error during AJAX call. Please try again");
  45.          }
  46.        }
  47.     }
  48.  
  49.  
and the servlet (doPost()) looks like this :


Expand|Select|Wrap|Line Numbers
  1.  
  2.               if(request.getParameter("value1") != null){
  3.             value = request.getParameter("value1");
  4.         }  
  5.         else  
  6.             value = "";
  7.  
  8.         response.setContentType("text/html");
  9.  
  10.         response.getWriter().write("helllo");
  11.  
I however, do not understand what is wrong, do I need to specify the name of the html file just like we do in case of RequestDispatcher() methods. I dont think that is the case here ?
Nov 4 '08 #1
Share this Question
Share on Google+
1 Reply


acoder
Expert Mod 15k+
P: 16,027
Since you seem to be having a problem with the servlet, I've moved this thread to the Java forum.

Moderator.
Nov 4 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.