java中如何用response编写一个刷新页面,实现每两秒钟刷新一次?

java中如何用response编写一个刷新页面,实现每两秒钟刷新一次?,第1张

<%@ page language="java" contentType="text/htmlcharset=gb2312" import="java.util.*" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

</head>

<body>

<%

response.addIntHeader("Refresh",2)

%>

<h4>2秒刷新一次</h4>

</body>

</html>

import java.awt.Toolkit

import java.util.Calendar

import javax.swing.*

public class Time extends JFrame implements Runnable{

int year = 0

int moth = 0

int day = 0

int hour = 0

int minute = 0

int second = 0

int width = Toolkit.getDefaultToolkit().getScreenSize().width

int height = Toolkit.getDefaultToolkit().getScreenSize().height

JLabel jl1 = null

public Time(){

Calendar c=Calendar.getInstance()//获得系统当前日期

year=c.get(Calendar.YEAR)

moth = c.get(Calendar.MONTH)+1//系统日期从0开始算起

day=c.get(Calendar.DAY_OF_MONTH)

hour = c.get(Calendar.HOUR)//小时

minute =c.get(Calendar.MINUTE)//分

second = c.get(Calendar.SECOND)//秒

jl1 = new JLabel(year + "年" + moth + "月" + day + "日" + hour+ "时" + minute + "分" + second + "秒")

jl1.setBounds(200, 100, 100, 50)

this.add(jl1)

this.setTitle("时间线程")

this.setLocation((width - 500) / 2, (height - 500) / 2)

this.setSize(500, 500)

this.setVisible(true)

}

public static void main(String[] args) {

Runnable aRunnable = new Time()

new Thread(aRunnable).start()

}

@Override

public void run() {

while(true){

try {

Calendar c=Calendar.getInstance()//获得系统当前日期

year=c.get(Calendar.YEAR)

moth = c.get(Calendar.MONTH)+1//系统日期从0开始算起

day=c.get(Calendar.DAY_OF_MONTH)

hour = c.get(Calendar.HOUR)//小时

minute =c.get(Calendar.MINUTE)//分

second = c.get(Calendar.SECOND)//秒

jl1.setText(year + "年" + moth + "月" + day + "日" + hour+ "时" + minute + "分" + second + "秒")

Thread.sleep(1000)

} catch (InterruptedException e) {

e.printStackTrace()

}

}

}

}

<h1>当前时间: <%=new java.util.Date() %></h1>

<%response.setHeader("Refresh","1")%>

JSP页面加上 <%response.setHeader("Refresh","1")%>1为时隔多久刷新,单位S。上面是我的例子,两端代码代表刷新时间,每秒刷新一下浏览器


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12040565.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存