AJAX problem

I am new to AJAX and trying to build a sample application.

When I run the same I am getting the following error message:

Error 404: No target servlet configured for uri: /AjaxServlet

My HTML page code:

<HTML>

<BODY>

<script type="text/javascript">

function showHint(str){

if (str.length==0){

document.getElementById("txtHint").innerHTML="";

return;

}

xmlHttp=GetXmlHttpObject()

if (xmlHttp==null){

alert ("Your browser does not support AJAX!");

return;

}

var url="/AjaxServlet";

url=url+"?q="+str;

url=url+"&sid="+Math.random();

xmlHttp.onreadystatechange=stateChanged;

xmlHttp.open("GET", url,true);

xmlHttp.send(null);

}

function GetXmlHttpObject(){

var xmlHttp=null;

try{// Firefox, Opera 8.0+, Safari

xmlHttp=new XMLHttpRequest();

}catch (e){// Internet Explorer

try{

xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");

}catch (e){

xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");

}

}

return xmlHttp;

}

function stateChanged(){

if (xmlHttp.readyState==4){

if(xmlHttp.status==200)

{

var message = xmlHttp.responseXML.getElementsByTagName("valid")[0].childNodes[0].nodeValue;

document.getElementById("txtHint").innerHTML=message;

}else

{

document.getElementById("txtHint").innerHTML=xmlHttp.responseText;

//alert("Error loading pagen"+ xmlHttp.status + ":"+xmlHttp.statusText);

}

}

}

</script>

<form>

First Name:

<input type="text" id="txt1" onkeyup="showHint(this.value)">

</form>

Suggestions:

<span id="txtHint">

</span>

</BODY>

</HTML>

My web.xml code:

.............................................................

.............................................................

.............................................................

<servlet>

<servlet-name>AjaxServlet</servlet-name>

<servlet-class>com.abcxyz.coa.testAJAX.AjaxServlet

</servlet-class>

<load-on-startup>-1</load-on-startup>

</servlet>

.............................................................

.............................................................

.............................................................

<servlet-mapping>

<servlet-name>AjaxServlet</servlet-name>

<url-pattern>/AjaxServlet</url-pattern>

</servlet-mapping>

.............................................................

.............................................................

.............................................................

My servlet code:

package com.abcxyz.coa.testAJAX;

import java.io.IOException;

import javax.servlet.Servlet;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

publicclass AjaxServletextends HttpServletimplements Servlet{

protectedvoid doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{

System.out.println("Entering doGet");

String targetId = request.getParameter("q");

if (targetId !=null){

response.setContentType("text/xml");

response.setHeader("Cache-Control","no-cache");

response.getWriter().write("<valid>true</valid>");

}else{

response.setContentType("text/xml");

response.setHeader("Cache-Control","no-cache");

response.getWriter().write("<valid>false</valid>");

}

System.out.println("Exiting doGet");

}

protectedvoid doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{

System.out.println("Entering doPost");

doGet(request, response);

System.out.println("Exiting doPost");

}

}

Now when I enter something on my 'First Name' text field, the following error message is being displayed.

Error 404: No target servlet configured for uri: /AjaxServlet

Any help is highly appreciated.

SirG

[7567 byte] By [SirGenerala] at [2007-11-27 5:02:05]
# 1

:) well itz bound to happen

var url="AjaxServlet";

url=url+"?q="+str;

url=url+"&sid="+Math.random();

this is how you have to call the servlet...

Btb is it that nessary to use

<load-on-startup>-1</load-on-startup>

if it is all about calling intializing some settings why don't you go by 01,2,3..... ? (i don't think this aspect wud be a cause of concern but the URL is)

Hope this might help :)

REGARDS,

RaHuL

RahulSharnaa at 2007-7-12 10:19:49 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Thanks Dude!!!It works wonderfully... Just changed it to var url="AjaxServlet";and removed the <load-on-startup>-1</load-on-startup> and everything works fine.SirG
SirGenerala at 2007-7-12 10:19:49 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...