IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

二十一、JSP 自动刷新

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

JSP 自动刷新

想象一下,如果要直播比赛的比分,或股票市场的实时状态,或当前的外汇配给,该怎么实现呢?显然,要实现这种实时功能,您就不得不规律性地刷新页面。

JSP提供了一种机制来使这种工作变得简单,它能够定时地自动刷新页面。

刷新一个页面最简单的方式就是使用response对象的setIntHeader()方法。这个方法的签名如下:

public void setIntHeader(String header, int headerValue)

这个方法通知浏览器在给定的时间后刷新,时间以秒为单位。


页面自动刷新程序示例

这个例子使用了setIntHeader()方法来设置刷新头,模拟一个数字时钟:

<%@ page import="java.io.*,java.util.*" %>
<html>
<head>
<title>Auto Refresh Header Example</title>
</head>
<body>
<center>
<h2>Auto Refresh Header Example</h2>
<%    
// Set refresh, autoload time as 5 seconds    
response.setIntHeader("Refresh", 5);    
// Get current time    
Calendar calendar = new GregorianCalendar();    
String am_pm;    
int hour = calendar.get(Calendar.HOUR);    
int minute = calendar.get(Calendar.MINUTE);    
int second = calendar.get(Calendar.SECOND);    
if(calendar.get(Calendar.AM_PM) == 0)       
am_pm = "AM";    
else       
am_pm = "PM";    
String CT = hour+":"+ minute +":"+ second +" "+ am_pm;    
out.println("Crrent Time: " + CT + "\n"); 
%>
</center>
</body>
</html>

把以上代码保存在main.jsp文件中,访问它。它会每隔5秒钟刷新一次页面并获取系统当前时间。运行结果如下:

Auto Refresh Header Example
Current Time is: 9:44:50 PM

您也可以自己动手写个更复杂点的程序。

干货推荐

文章永久链接:https://tech.souyunku.com/?p=11038


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(68) 打赏



未经允许不得转载:搜云库技术团队 » 二十一、JSP 自动刷新

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367