[英]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.