寧可在後臺啓動ajax民意調查。
這是一個SSCCE與jQuery的一點幫助。只需copy'n'paste'n'run它(並更改Flash對象以適合您的實際代碼)。
<!doctype html>
<html lang="en">
<head>
<title>SO question 2290101</title>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(document).ready(function() {
setInterval(function() {
$.get('poll');
}, ${(pageContext.session.maxInactiveInterval - 10) * 1000});
});
</script>
</head>
<body>
<object type="application/x-shockwave-flash" ... >
...
</object>
</body>
</html>
這裏${pageContext.session.maxInactiveInterval}
返回秒剩餘會話尚未生活(並且已經一點點縮短 - 只是要及時與poll-並轉換爲毫秒,這樣它適合什麼setInterval()
預計)。 $.get('poll')
應該調用一個映射到url-pattern
的/poll
的servlet,並在doGet()
方法中基本包含以下行。
request.getSession(); // Keep session alive.
就是這樣。
下面是一個很好的解釋:http://ajaxpatterns.org/Heartbeat – Aito 2010-02-18 16:31:35