简体   繁体   中英

Java servlet doGet() not refreshing after first call

I am making AJAX calls to a Java servlet at a set interval. Inside the doGet it querys a DB, puts the results in JSON format, and returns them. This works great on the first try (when the page is first loaded or refreshed) but at every ajax call after it returns the same data as the first call even if things were added.

Servlet:

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    System.out.println("in");

    Connection con = null;
    Statement stmt = null;
    try {
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection(
                DB_URL, USER, PASS);
        stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("Select * from chatroom");
        JSONObject jsonPosts = new JSONObject();
        JSONArray postList = new JSONArray();
        JSONObject post;

        while (rs.next()) {
            post = new JSONObject();
            post.put("user", rs.getString("user"));
            post.put("text", rs.getString("text"));
            post.put("time", rs.getString("time"));

            postList.add(post);
        }

        System.out.println(postList.toString());

        response.setContentType("application/json");

        response.getWriter().write(postList.toString());

        stmt.close();
        con.close();
    } catch (ClassNotFoundException cnfe) {
        cnfe.printStackTrace();
    } catch (SQLException sqe) {
        sqe.printStackTrace();
    }

}

Javascript:

            setInterval(function () {

                getChat();

            }, 6000);


                function getChat() {

                var posts;
                $.ajax({
                    type: "GET",
                    url: "getChat",
                    data: "position=hi",
                    datatype: "json",
                    success: function (data) {



                        for (var i = 0; i < data.length; i++)
                        {
                            $("#chatWindow").append(data[i].text + "<br/>");
                        }
                    }

                });


            }

Getting the same data when repeating the same requests usually is an issue with a cache (let it be in the browser, the server or in a proxy).

Several ways of avoiding cache's effect:

  • Disable them (generally not a good idea since it causes a performance impact).
  • Use HTTP headers that signal to the browser that the content should not be cached / that previous cached data is stale.
  • For AJAX, use the cache: false parameter.
  • If all of the above fails, add a bogus parameter to the URL so the browser interprets it as a request of a different resource(vg, url: "getChat" + (new Date()).getTime() )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM