简体   繁体   English

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

[英]read updated data on client from file created by server JSP

I am creating a web application in java. 我正在用Java创建一个Web应用程序。 On client side I have a bar chart that display some data stored in a tsv file created by the java server page. 在客户端,我有一个条形图,显示一些由Java服务器页面创建的tsv文件中存储的数据。 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: 这是我的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("");


    }

}

This is the javascript code that calls the servlet: 这是调用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) {
......

In the html page I have also put this: 在html页面中,我也输入了以下内容:

<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. 有了此代码,即使文件tsv中的数据发生更改,我也可以在第一次调用servlet时显示正确的数据。

Which is the best way to read the refreshed data in the file? 读取文件中刷新数据的最佳方法是哪种?

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 1)您可以创建一个过滤器,并在过滤器中指示不要缓存这样的内容,以防止用户注销后看到以前访问过的安全页面

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) 2)每次您访问tsv文件的url时,请在末尾添加一个随机变量。(通常是在Internet exlorer的情况下)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从远程服务器上的jsp文件读取文件数据 - Cannot read file data from jsp file on remote server Weblogic从服务器下载文件并在JSP中读取 - Weblogic download file from server and read in JSP 如何使用JSP从客户端读取excel文件并处理数据而不进行存储 - How to read excel file from client using JSP and process data without storing 将数据写入 JSON 文件,将 JSON 文件从客户端发送到服务器,读取 JSON - Write data in JSON file, send JSON file from client to server, read JSON 如何在服务器端而非客户端的JAVA中逐行从config.properties文件读取数据 - How to read data from config.properties file line by line in JAVA on the server side not on the client side JSP:在客户端上保存文件而不将其存储在服务器上 - JSP: save a file on client without storing it on server 如何从服务器端代码(JSP或Servlet)读取客户端本地磁盘上的文件 - How to read files on client local disk from server side code (JSP or Servlet) 读取JSP文件并生成HTML-而不是通过Web服务器 - Read a JSP file and produce HTML - not with a web server 在JSP中从服务器到客户端获取事件 - Get events from server to client in jsp 从服务器JSP获取客户端IP [安全性] - Get Client IP from a Server JSP [Security]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM