473,388 Members | 1,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Send data from doPost() in servlet to AJAX

sid0404
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
1 8098
acoder
16,027 Expert Mod 8TB
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

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

Similar topics

4
by: BigAl | last post by:
I am using the following code in a servlet to send data back to a J2ME application:
5
by: miyuru | last post by:
Hi, I need to send a file that is selected from a input type = file html control to the server through a ajax method. Please help Thanks & Regards Miyuru Ratnayake
4
by: Dennis E. Jones, Jr. | last post by:
I need to post an ASP.NET form between sites for report processing. Site A contains the asp.net forms and site B contains Java Servlets for report processing. All form fields values must be...
2
by: Mux | last post by:
I am facing the following problem while exporting data to Word. The current implementation is as described below: I have a JSP file which has a link that enables you to export the data to Word....
12
by: Juan Carlos Espinoza | last post by:
i have a problem com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data truncated for column 'FGiroNPagDol' at row 1 at...
5
by: xirowei | last post by:
i'm newbie in java servlet, how to let public void doPost can access to public void doGet, stringLength variable? below is my code: import javax.servlet.*; import javax.servlet.http.*; import...
1
by: vaskarbasak | last post by:
Hi All, 1.Why do we override the doGet and/or doPost methods instead of the service method? 2. We shouldn't synchronize the service method. but what will happen if we do so, except from the...
2
by: khalidanwar123 | last post by:
i am getting the following error while updating a clob field. ERROR java.sql.SQLException: Data size bigger than max size forthis type: 4003 19:28:27,499 ERROR at...
9
by: gavy7210 | last post by:
hello friends i am new to struts. i want to add the user information in the database and then display it back to him. i am using struts 1.2,Eclipse Platform Version: 3.4.2,mySql 5.0.1,jdk...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.