View Full Version : problem in calling servlet from javascript using xmlhttpreq

01-20-2009, 11:09 AM
Hello Experts,
I want to send xml data to servlet using xmlhttprequest object from a pure HTML Page.I have written some code but it is not working.One strange thing if I copy the same code in jsp & then run it , it is calling servlet from javascript & sending the xml data to it.I hope I am able to express my problem.Please Help.Thanx in advance.

This is my code

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>

<script type="text/javascript">
var xmlHttp;

function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();

function doSyncResult() {
queryString ="http://localhost:8090/TestProject1/receiveXMLData";

var params = "xml=<cstore> <customer id='1'><name> Neha</name></customer> </cstore>";

xmlHttp.open("POST", queryString, true);

xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", params.length);
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.onreadystatechange = handleStateChange;



function handleStateChange() {

if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {

alert("results sync properly");

<input type="button" onClick="doSyncResult()">


01-20-2009, 04:17 PM
xmlhttp requests are sent to a server and hence require the code to be served out of a server...saving the code as a html file and calling the server isn't going to work