简体   繁体   English

如何自动刷新Java servlet页面

[英]How to refresh the Java servlet page automatically

I am developing a web application based on a Java servlet. 我正在开发基于Java Servlet的Web应用程序。

Behind the Java servlet, a thread works. 在Java servlet的背后,有一个线程起作用。 The Java servlet outputs html and java script codes. Java Servlet输出html和Java脚本代码。 Thus, html and java script codes dynamically vary by a thread. 因此,HTML和Java脚本代码会因线程而动态变化。

I made the following codes. 我做了以下代码。 html and javascript codes are output by response.getWriter().println() . html和javascript代码由response.getWriter().println()

The problem is; 问题是; when a thread is completed, nothing is shown. 当线程完成时,什么也没有显示。 However, when I manually refresh the page (Ctrl-R) after a thread is done, I get the page that I expected. 但是,当我在完成线程后手动刷新页面(Ctrl-R)时,得到的是我期望的页面。

<head>
<script type=\"text/javascript\">
var auto_refresh = setInterval(
function start()
{
$('#load_latest').load('./PubRecommendation' + ' #load_latest');
}, 2000);
</script>           
</head>


if (result == null) {
    // the variable result is received from a behind thread every 2 seconds. 
    // while a thread is processed
    <div id=\"load_latest\">
    <p>Currently processing. <p>
    </div>
} else {
    // a thread is completed
    <div id="refresh">
    <p>Process is done. <p>
    </div>
}  

I already tried location.reload() , but it did not work. 我已经尝试过location.reload() ,但是没有用。

You can use this to refresh the page I have used this before just put this at the end of your html or jsp page: 您可以使用它来刷新我使用过的页面,然后再将其放在html或jsp页面的末尾:

<script type="text/javascript">
var cookie = ("" + document.cookie);
if(!cookie.match(/\S/))
{
    document.cookie = "refresh";
    location.reload(true);
}
else
{
    document.cookie = "refresh; expires=Thu, 01 Jan 1970 00:00:00 GMT";
}
</script>

This will set a cookie the first time the page is loaded then will automatically refresh and it will check the cookie and if it's been refreshed once then it will not reload the second time and will delete the cookie. 这将在第一次加载页面时设置一个cookie,然后将自动刷新并检查cookie,如果一次刷新,则不会第二次重新加载并删除co​​okie。

The term is Meta Refresh . 该术语是Meta Refresh Searching Google will give you enough examples. 搜索Google将为您提供足够的示例。

For example: 例如:

<meta http-equiv="refresh" content="5; url=/PubRecommendation#load_latest">

Would load your page in 5 seconds after the last load completed. 上次加载完成后,将在5秒钟内加载页面。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM