0

I am creating a web application in java. On client side I have a bar chart that display some data stored in a tsv file created by the java server page. By clicking on a button the server updates these data in the file. Now I want to read the refreshed data, but I get the older ones. It seems that the browser cached the file so it can't get the changed file.

This is my servlet code:

public class GetDataServlet extends HttpServlet 
{
    private static final long serialVersionUID = 1L;

    private User user;
    Utility utility; 

    public void init() throws ServletException {
        reset();
    }

    public void doGet (HttpServletRequest request,HttpServletResponse response)  throws ServletException, IOException {
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        PrintWriter out = response.getWriter();
        user.getProfile().get(0).setWeigth(user.getProfile().get(0).getWeigth()+0.03);
        user.getProfile().get(1).setWeigth(user.getProfile().get(1).getWeigth()+0.02);
        user.getProfile().get(5).setWeigth(user.getProfile().get(5).getWeigth()+0.01);
        utility.createTsvFile(user, "/usr/local/apache-tomcat-7.0.50/webapps/Visualizer/data.tsv");
        String message = String.format("data.tsv");
        i++;
        out.print(message);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if(request.getParameter("reset").compareTo("yes")==0)
            reset();
    }

    private void reset(){
        List<Concept> children = new ArrayList<Concept>();

        Concept food = new Concept();
        food.setWeigth(0.10);
        food.setLabel("food");
        food.setColor("#98abc5");

        Concept dish = new Concept();
        dish.setWeigth(0.08);
        dish.setLabel("dish");
        dish.setParent(food);
        dish.setColor("#8a89a6");

        Concept cuisine = new Concept();
        cuisine.setWeigth(0.06);
        cuisine.setLabel("cuisine");
        cuisine.setParent(food);
        cuisine.setColor("#8a89a6");

        children.add(dish);
        children.add(cuisine);
        food.setChildren(children);

        children.clear();

        Concept pizza = new Concept();
        pizza.setWeigth(0.05);
        pizza.setLabel("pizza");
        pizza.setParent(dish);
        pizza.setColor("#6b486b");

        Concept spaghetti = new Concept();
        spaghetti.setWeigth(0.05);
        spaghetti.setLabel("spaghetti");
        spaghetti.setParent(dish);
        spaghetti.setColor("#6b486b");

        Concept sushi = new Concept();
        sushi.setWeigth(0.06);
        sushi.setLabel("sushi");
        sushi.setParent(dish);
        sushi.setColor("#6b486b");

        children.add(pizza);
        children.add(spaghetti);
        children.add(sushi);

        dish.setChildren(children);



        List<Concept> profile = new ArrayList<Concept>();
        profile.add(food);
        profile.add(dish);
        profile.add(cuisine);
        profile.add(pizza);
        profile.add(spaghetti);
        profile.add(sushi);

        user = new User("mario", profile);
        utility = new Utility("");


    }

}

This is the javascript code that calls the servlet:

    function ajaxSyncRequest(reqURL) {
    //Creating a new XMLHttpRequest object
    var xmlhttp;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest(); //for IE7+, Firefox, Chrome, Opera, Safari
    } else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); //for IE6, IE5
    }
    //Create a asynchronous GET request
    xmlhttp.open("GET", reqURL, false);
    xmlhttp.send(null);

    //Execution blocked till server send the response
    if (xmlhttp.readyState == 4) {
        if (xmlhttp.status == 200) {
            document.getElementById("message").innerHTML = xmlhttp.responseText;
            update(xmlhttp.responseText);
            //alert(xmlhttp.responseText);
        } else {
            alert('Something is wrong !!');
        }
    }
}

function update(file){
    d3.tsv(file, function(error, data) {
......

In the html page I have also put this:

<meta http-equiv="Cache-control" content="no-cache">

but it doesn't work.

With this code I can display the correct data the first time I call the servlet, than even if the data in the file tsv changes I get the first one.

Which is the best way to read the refreshed data in the file?

Mario Lepore
  • 307
  • 1
  • 7
  • 17
  • 1
    Maybe help at http://stackoverflow.com/questions/1046966/whats-the-difference-between-cache-control-max-age-0-and-no-cache – rickz Feb 13 '14 at 16:46
  • I have tried no cache and max-age=0 with no resut. – Mario Lepore Feb 13 '14 at 17:02
  • You're creating the tsv file and then outputting "data.tsv" to the browser. In other words, you are not returning the tsv file in the response. Is this what you intended? – Taylor Feb 13 '14 at 17:33
  • I have added additional information to the question. My problem is that I read the correct data only the first time I call the servlet. Then I read always these data even if they are changed in the file. – Mario Lepore Feb 13 '14 at 19:54

1 Answers1

0

Okay I you are facing problem from browser caching problem like this so two hings can be done

1)You can create a filter and instruct in filter not to cache some what like this Prevent user from seeing previously visited secured page after logout

2)Each time you are hitting the url of tsv file add a random variable in end .(This is usually in case of internet exlorer)

Community
  • 1
  • 1
Dhruv Pal
  • 635
  • 7
  • 22