dwjohnston
dwjohnston

Reputation: 11852

AJAX HttpRequest doesn't recieve updated data from servlet

Servlet:

package world.hello;

import java.io.IOException;
import java.io.PrintWriter;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import world.hello.MyMainClass;

public class TestServlet extends HttpServlet{

  private static final int BYTES_DOWNLOAD = 1024;  

    public void doGet(HttpServletRequest request, 
           HttpServletResponse response) throws IOException
           {

            response.setContentType("text/plain");      

            OutputStream os = response.getOutputStream();
            os.write(("hello world"+Double.toString(Math.random())).getBytes());
            os.flush();
            os.close(); 
 }        
       public void doPost(HttpServletRequest request, 
               HttpServletResponse response) throws IOException
               {

                doGet(request, response);
      }

}

HTML:

<html>
<body>
<script>
function myAjax()
{
    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.open("GET","RunQuery", false); 
    xmlhttp.send(); 

    document.getElementById("myText").innerHTML=xmlhttp.responseText + " " + xmlhttp.readyState.toString() + " " + xmlhttp.status.toString() ;
    document.getElementById("myText2").innerHTML=Math.random();

}

</script>
    <button id = "myButton" onclick = "myAjax()">click me</button>

    <div id = "myText"></div>
        <div id = "myText2"></div>
</body>
</html>

If I access the servlet directly at http://localhost:9070/test_web_project_1/RunQuery

Each time I refresh it, I get a different random float displayed.

When I run the HTML at http://localhost:9070/test_web_project_1/myxjax.html, The second float changes, the first is fixed.

What is causing this, and how do i resolve it?

Upvotes: 0

Views: 31

Answers (1)

developerwjk
developerwjk

Reputation: 8659

Nevermind what I said before...your code is synchronous because you set async to false. Your issue is just browser caching. Your ajax request is being cached. You can trick the browser to not load the cache by adding a parameter with the date/time to the request like:

var d = new Date();
xmlhttp.open("GET","RunQuery?ts="+d.getTime(), false); 

That just makes the browser see each request as unique; there's no need to do anything with that param on the server side.

Or, you could add no-cache headers in the servlet being called by the Ajax. You can also do both to be extra cautious.

 response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
 response.setHeader("Pragma","no-cache"); //HTTP 1.0
 response.setDateHeader ("Expires", 0);

Upvotes: 1

Related Questions