By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,626 Members | 1,929 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,626 IT Pros & Developers. It's quick & easy.

Problem in ajax call

P: 2
Hi all

I am making a very simple ajax call. But I am not getting any message from sever.

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3.  
  4. <script type="text/javascript">
  5. function ajaxFunction()
  6. {
  7. var xmlhttp;
  8. if (window.XMLHttpRequest)
  9.   {
  10.   // code for IE7+, Firefox, Chrome, Opera, Safari
  11.   xmlhttp=new XMLHttpRequest();
  12.   }
  13. else if (window.ActiveXObject)
  14.   {
  15.   // code for IE6, IE5
  16.   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  17.   }
  18. else
  19.   {
  20.   alert("Your browser does not support XMLHTTP!");
  21.   }
  22. xmlhttp.onreadystatechange=function()
  23. {
  24. alert("ready state:"+xmlhttp.readyState+",status:"+xmlhttp.status+",response: "+xmlhttp.responseText);
  25.  
  26. }
  27. xmlhttp.open("GET","login1.html",true);
  28. xmlhttp.send("");
  29. }
  30. </script>
  31.  
  32. <form name="myForm">
  33. Name: <input type="text" name="username"  />
  34. Time: <input type="text" name="time" />
  35. <button onclick="ajaxFunction()">Click to ajax</button>
  36. </form>
  37.  
  38. </body>
  39. </html>
  40.  
Expand|Select|Wrap|Line Numbers
  1. ------------------------------------------------Server class------------------------
  2. package com.test;
  3.  
  4. import java.io.IOException;
  5. import java.io.PrintWriter;
  6.  
  7. import javax.servlet.ServletException;
  8. import javax.servlet.http.HttpServlet;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11.  
  12. public class DemoServlet extends HttpServlet {
  13.  
  14.     private static final long serialVersionUID = 93218348027333012L;
  15.  
  16.     protected void doGet(HttpServletRequest req, HttpServletResponse res)
  17.             throws ServletException, IOException {
  18.         System.err.println("Calling servlet12345");
  19.         PrintWriter writer = res.getWriter();
  20.         writer.println("From server");
  21.         writer.close();
  22.  
  23.     }
  24.  
  25.     protected void doPost(HttpServletRequest req, HttpServletResponse res)
  26.             throws ServletException, IOException {
  27.  
  28.         doGet(req, res);
  29.     }
  30.  
  31. }
  32. --------------------------------------------------------------------------------
  33.  
Oct 7 '09 #1
Share this Question
Share on Google+
3 Replies


acoder
Expert Mod 15k+
P: 16,027
Are you sure the server-side file is called login1.html?
Oct 7 '09 #2

P: 2
Hi
Thanks for considering my issue. Acutally login1.html is servlet name defined in web.xml.
Expand|Select|Wrap|Line Numbers
  1. <servlet>
  2.   <display-name>DemoServlet</display-name> 
  3.   <servlet-name>DemoServlet</servlet-name> 
  4.   <servlet-class>com.test.DemoServlet</servlet-class> 
  5.   </servlet>
  6. - <servlet-mapping>
  7.   <servlet-name>DemoServlet</servlet-name> 
  8.   <url-pattern>/login1.html</url-pattern> 
  9.   </servlet-mapping>
Oct 7 '09 #3

acoder
Expert Mod 15k+
P: 16,027
Change this line:
Expand|Select|Wrap|Line Numbers
  1. <button onclick="ajaxFunction()">Click to ajax</button>
to
Expand|Select|Wrap|Line Numbers
  1. <input type="button" onclick="ajaxFunction()" value="Click to ajax">
Oct 9 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.