473,400 Members | 2,145 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,400 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 8100
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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:
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.