views:

75

answers:

3

I am trying to do a simple application using Ajax and servlet. But when i was trying to execute the application, it is not working. Please go through the code and let me know what would be the problem?

function createRequest()
 {
  alert("hai createRequest()");
  try
  {
   req=new XMLHttpRequest();

  }
  catch(trymicrosoft)
  {
   try
   {
    req=new ActiveXObject("Msxml2.XMLHTTP");
   }
   catch(othermicrosoft)
   {
    try
    {
     req=new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch(failed)
    {
     req=null;
    }
   }

  }
  if(req==null)
   alert("req==null");

 }
 function startRequest()
 {
  alert("hai startRequest()");
  createRequest();
  alert("hai created Request()");
  var username=document.getElementById("user").value;
  req.open("get","http://localhost:8080/login/CheckLogin?user="+username,true);
  req.onreadystatechange=handleStateChange;
  alert("hai returned from handle state change");
  req.send(null);

 }

 function handleStateChange()
 {
  alert("hai handleStateChange()");
  if(req.readyState==4)
  {
   if(req.status==200)
   {
    var message=req.responseXML.getElementsByTagName("valid")[0].childNodes[0].nodeValue;
    document.getElementById("results").innerHTML=message;
   }
   else
   {
    alert("Sorry status failed");
    document.getElementById("results").innerHTML="Sorry problem in status";
   }
  }
  else
  {
   alert("Sorry readyStatus failed");
   document.getElementById("results").innerHTML="Sorry problem in readyState";
  }
 }

The above functions wrote in Javascript and in servlet

package com.assignment.login;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class CheckLogin extends HttpServlet
{
    public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException
    {
     System.out.println("Hai i have entered into servlet");
     String userId=request.getParameter("user");
     if(userId!=null && !userId.equals("jeya"))
     {
      response.setContentType("text/xml");
      response.getWriter().write("<valid>U can use this id</valid>");
     }
     else
     { 
      response.setContentType("text/xml");
      response.getWriter().write("<valid>U cannot use this id</valid>");
     }
    }


    public void doPost(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException
    {
     doGet(request,response);
    }
}

But in the output the the following alerts are being printed,

Hai start request
Hai create request
Hai created request
Hai returned from handle state change
hai handle state change
sorry readyStatus  failed
hai handle state change
sorry readyStatus  failed
hai handle state change
sorry readyStatus  failed
hai handle state change
sorry status  failed

My web.xml,

<web-app>
<servlet>
 <servlet-name>CheckLogin</servlet-name>
 <servlet-class>com.assignment.login.CheckLogin</servlet-class>
</servlet>

<servlet-mapping>
 <servlet-name>CheckLogin</servlet-name>
 <url-pattern>/CheckLogin</url-pattern>
</servlet-mapping>

In the servlet i have printed "I have entered into servlet" and it is not getting printed in the server window.. So what would be the problem and please give me suggesstions to resolve it... Thanks in advance...

EDIT: this is the exception ocuring when i am trying to execute the above application.. But the class CheckLogin is at the right place... But the same code is working in eclipse. So please anyone tell me what the problem may be??? The requested resource (/login/CheckLogin) is not available.

+1  A: 

you are using the wrong URL:

req.open("get","http://localhost:8080/login/CheckLogin?user="+username,true);

i don't know what server you are using, but it will by sufficient to use CheckLogin:

req.open("get","CheckLogin?user="+username,true);

or req.open("get","/login/CheckLogin?user="+username,true);

Probably the server at localhost:8080 isn't reachable.

Salandur
Even it is showing the same...
i2ijeya
A: 

I think you have to define req variable in the top of the javascript page out side the functions. Like,

var req;

function yourFunctions(){

.
.
.
.


   }

Try that.

Manjula
Yeah i have initialized with the value null at the top and i was forgot to mention..
i2ijeya
+1  A: 

please check whether you have followed the naming convection correctly....because i think there no error in the code.

Hari
Thanks hari.. Yeah i have missed out the naming conventions for WEB-INF. Now its working fine..
i2ijeya