0

Hello guys I am new to ajax, trying to create a simple basic data transfer between servlet and html page using javascript(don't know jQuery). But that's not working tried reading tutorials but still can't figure out the problem. Can someone tell me where i am doing it wrong.

Html page index.html

<body>
    <button onClick="run()">Click</button>
    <script>
        function run() {
            if(window.XMLHttpResquest) xmlhttp=new XMLHttpResquest();
            else  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            xmlhttp.open("GET","/test",false);
            xmlhttp.send(null);
            xmlhttp.onreadystatechange=function() {
                if (xmlhttp.readyState==4 && xmlhttp.status==200) {
                    document.getElementById("demo").innerHTML=xmlhttp.responseText;
                }
            }
        }
    </script>
    <br>
    <p id="demo">Static</p>
</body>

Servlet : Test.java

package foo;

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

public class Test extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException {
        res.setContentType("text/plain");
        res.getWriter().write("Dynamic");
    }
}

WebServer i am using is tomcat, web.xml

<web-app>
    <servlet>
        <servlet-name>tst</servlet-name>
        <servlet-class>foo.Test</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>tst</servlet-name>
        <url-pattern>/test</url-pattern>
    </servlet-mapping>
</web-app>

Edit:

What problem I am facing is - when I click on the 'click button' the innerHTML of the paragraph element should change but it is not changing. I tried runnig the servlet from address bar to check for servlet errors it's working it prints "Dynamic " so not a problem with the servlet and Web.xml file.

1
  • what is not working,are you getting any error? Commented Jan 18, 2015 at 12:35

1 Answer 1

2

Write 'xmlhttp.send(null)' after 'onreadystatechange' function. Hopefully it should work. Moreover, you don't need to pass 'null' in send.

Following code works for me perfectly file. Another change I made is to add '.' period in URL before '/' in open(). And I called the send() after declaring the function for onreadystatechange.

<body>
    <button onClick="run()">Click</button>
     <script>
        function run() {
            if(window.XMLHttpResquest) xmlhttp=new XMLHttpResquest();
            else  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            xmlhttp.open("GET","./test",false);
            xmlhttp.onreadystatechange=function() {
                if (xmlhttp.readyState==4 && xmlhttp.status==200) {
                    document.getElementById("demo").innerHTML=xmlhttp.responseText;
                }
            }
            xmlhttp.send();
        }
    </script>
    <br>
    <p id="demo">Static</p>
</body>
Sign up to request clarification or add additional context in comments.

4 Comments

Moved the line below the function still not working.
I've also modified the url.. check "xmlhttp.open("GET","./test",false);"
:) pretty strange.. the code which I pasted works fine, for sure.. have you used firebug or IE developer tools to track the network traffic and JS debug? That will be helpful.
It's true in xmlhttp.open for asynchronous it worked. Change it to true in your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.