JSP中使用response设置HTTP头信息

本文介绍如何利用HTTP响应头信息中的refresh属性实现页面的自动刷新与定时跳转功能,并提供了具体的JSP示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用response对象可以设置HTTP的头信息。
格式:response.setHeader("头信息名称","参数");
其中一个重要的头信息是:refresh(刷新)。

实例:每秒刷新一次页面,显示刷新次数

response01.jsp:
程序代码 程序代码

<%@page contentType="text/html;charset=GB2312" %>
<html>
<head>
<title>response01.jsp</title>
</head>

<body>
<%!int i=0; %>
<%
//每秒刷新一次
response.setHeader("refresh","1");
%>
<%=i++ %>
</body>
</html>


使用HTTP头信息设置,我们还可以实现在规定的时间内跳转:
格式:response.setHeader("refresh","时间;URL=跳转页面地址");

实例:打开response02.jsp,2秒之后跳转到response03.jsp:

response02.jsp:
程序代码 程序代码

<%@page contentType="text/html;charset=GB2312" %>
<html>
<head>
<title>response02.jsp</title>
</head>

<body>
<%
response.setHeader("refresh","2;URL=response03.jsp");
%>
<h1>两秒后跳转到response03.jsp</h1>
如果没有跳转,请点<a href="response03.jsp">这里</a>
</body>
</html>


response03.jsp:
程序代码 程序代码

<%@page contentType="text/html;charset=GB2312" %>
<html>
<head>
<title>response03.jsp</title>
</head>

<body>
<h1>Welcome to response03.jsp!</h1>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值