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



我正在用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)您可以创建一个过滤器并指示过滤器不要缓存一些类似的东西 防止用户在注销后看到以前访问过的安全页面

2)每次点击 tsv 文件的 url 时,都会在末尾添加一个随机变量。(这通常是在互联网搜索者的情况下)

最新更新