Mohamed Iqzas
Mohamed Iqzas

Reputation: 1046

Ajax example program not working

I am trying to make an AJAX request(from New.html) and process that request in a servlet ( in PropertyReader.java). Here for the get request, i am just trying to print to the console to see whether i am able to get the AJAX request in doGet() method but nothing gets printed. means the doGet method is not getting called. I am sending out the AJAX request on clicking a button in html page. please help me. what I have missed I'm not sure.

My Servlet: PropertyReader.java

package org.jboss.samples.webservices;
public class PropertyReader extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public PropertyReader() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    System.out.println("************getting GET request");
}

}

My html page with AJAX call: New.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"   "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Status Report</title>
<Script>
function getIt()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
      alert("ready");
    document.getElementById("fill").innerHTML=xmlhttp.responseText;
    }
  }

xmlhttp.open("GET","/PropertyReader?property="+document.getElementById("property").value,true);
 xmlhttp.send();

alert("property2="+document.getElementById("property").value);

return false;
}
</script>


</head>
<body>
Hi !!

<a href="pdf">pdf</a>

<form >
<input id="property" type="text" width="50">

<input type="button" value="Get It!" onclick="getIt();">

</form>

<div id="fill" style="font:20px; color:red;">
who am i?
</div>

</body>
</html>

My DD: web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>TestWebb</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    <welcome-file>New.html</welcome-file>
  </welcome-file-list>
  <servlet>
    <display-name>HelloWorld</display-name>
    <servlet-name>HelloWorld</servlet-name>
    <servlet-class>org.jboss.samples.webservices.HelloWorld</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>HelloWorld</servlet-name>
    <url-pattern>/HelloWorld</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>FileServer</display-name>
    <servlet-name>FileServer</servlet-name>
    <servlet-class>org.jboss.samples.webservices.FileServer</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>FileServer</servlet-name>
    <url-pattern>/pdf</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>PropertyReader</display-name>
    <servlet-name>PropertyReader</servlet-name>
    <servlet-class>org.jboss.samples.webservices.PropertyReader</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>PropertyReader</servlet-name>
    <url-pattern>/PropertyReader/*</url-pattern>
  </servlet-mapping>
</web-app>

Upvotes: 0

Views: 289

Answers (2)

Kuldeep Choudhary
Kuldeep Choudhary

Reputation: 775

Remove first "/ " from url then it may work fine Like

xmlhttp.open("GET","PropertyReader?property="+document.getElementById("property").value,true);

i think it will work fine if you given all file name correctly

or you can check it using other http status also

 xmlhttp.onreadystatechange=function()
 {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
   {
      alert("ready");
      document.getElementById("fill").innerHTML=xmlhttp.responseText;
   }
   else if(xmlhttp.status == 400)
   { 
            alert ('bad status');
    }
    // for other status also for checks working
}

Upvotes: 1

kcak11
kcak11

Reputation: 842

Could be the problem in your xmlhttp.open invocation. Try to use absolute path i.e http://...... for the url instead of a relative url.

Also to check whether ajax is happening or not, you can use the developer tools in the browser by pressing F12 key and monitoring the network section.

Upvotes: 0

Related Questions