Simple ajax example using java - problem while receving the response from the servlet
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Simple ajax example using java - problem while receving the response from the servlet

  1. #1
    Join Date
    Apr 2009
    Posts
    4

    Simple ajax example using java - problem while receving the response from the servlet

    Hello,

    I am trying learn ajax while implementing a simple ajax example using java.

    Following is my index.html page where I am trying to use ajax and get some text from the servlet
    and display it in the <div>.


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE> Ajax Test Page </TITLE>
    <script type="text/javascript">

    function register()
    {
    var req;

    if (window.XMLHttpRequest)
    {
    req = new XMLHttpRequest( );
    }
    else if (window.ActiveXObject)
    {
    req = new ActiveXObject("Microsoft.XMLHTTP");
    }

    var url = "register";
    req.onreadystatechange = response;
    req.open("GET", url, true);
    req.send(null);
    }

    function response()
    {
    if (req.readyState==4)
    {
    if (req.status == 200)
    {
    var time = req.responseText;
    alert(time);
    document.getElementById("register").innerHTML = req.responseText;
    }
    }
    }
    </script>
    </HEAD>

    <BODY>

    <a href="" onClick="register();">Register</a>
    <div id="register"> </div>
    </BODY>
    </HTML>


    Following is my servlet where I am sending a text "First Name" in response to the ajax call.

    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;

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

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
    response.setContentType("text/html");
    response.getWriter().write("First Name");
    System.out.println("inside doget");
    }
    }


    When I run this application, I can see that the request is being sent to the servlet (RegisterForm)
    "System.out.println("inside doget");" is executed, "inside doget" is printed on to the Tomcat console but the response is not caught in the response() javascirpt method.

    Can anyone please tell me where am I going wrong?

  2. #2
    Join Date
    Feb 2008
    Posts
    966

    Re: Simple ajax example using java - problem while receving the response from the ser

    First off, this should probably be in the client side scripting forumn.

    However, normally you need to set your response ready state like this:

    request.onreadystatechange = function() { response(); };

    My guess is that you are never getting into that method and you probably have a javascript error (lower left hand corner of IE) when you try to run this.

    At the very top of your response() method set an alert("Hi"); and see if you are even getting there.

  3. #3
    Join Date
    Apr 2009
    Posts
    4

    Re: Simple ajax example using java - problem while receving the response from the ser

    Hello Program This,

    First off all, thanks for replying to my post. This is the first time I have ever used a forum. So I am sorry for posting it here.

    I followed your suggestions. Modified the code as


    req.onreadystatechange = function ()
    {
    if (req.readyState==4)
    {
    if (req.status == 200)
    {
    var time = req.responseText;
    alert(time);
    document.getElementById("register").innerHTML = req.responseText;
    }
    }
    }


    and it worked.

    I was following the example at http://www.w3schools.com/Ajax/ajax_source.asp where it says

    xmlHttp.onreadystatechange=stateChanged;

    for calling the stateChanged function.

    Thanks once again.

  4. #4
    Join Date
    Feb 2008
    Posts
    966

    Re: Simple ajax example using java - problem while receving the response from the ser

    That'll teach you a thing about w3schools

    Good reference, but not always quite accurate. Glad I could help.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center