从服务器JSP创建的文件中读取客户端上的更新数据

时间:2014-02-13 16:39:18

标签: java jsp servlets d3.js browser-cache

我正在用java创建一个Web应用程序。在客户端,我有一个条形图,显示存储在由java服务器页面创建的tsv文件中的一些数据。通过单击按钮,服务器将更新文件中的这些数据。现在我想读取刷新的数据,但是我得到了更旧的数据。似乎浏览器缓存了该文件,因此无法获取更改的文件。

这是我的servlet代码:

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("");


    }

}

这是调用servlet的javascript代码:

    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) {
......

在html页面中我也提到了这个:

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

但它不起作用。

使用这段代码,我第一次调用servlet时可以显示正确的数据,即使文件tsv中的数据发生变化,我也会得到第一个。

哪种方法可以读取文件中刷新的数据?

1 个答案:

答案 0 :(得分:0)

好的,我正面临着像这样的浏览器缓存问题,所以可以完成两个小时

1)您可以创建一个过滤器并指示过滤器不要缓存某些类似Prevent user from seeing previously visited secured page after logout

的内容

2)每次你点击tsv文件的url时,最后添加一个随机变量。(这通常是互联网浏览器的情况)