jsp实现respond页面的刷新与跳转功能


respond页面刷新效果:


<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<%
	Date date = new Date();
	//String dateFormatStr="yyyy-M-dd\n\tH:mm:ss";
	String dateFormatStr="yyyy:MM:dd HH:mm:ss";
	SimpleDateFormat sdf = new SimpleDateFormat(dateFormatStr);
	out.print("<h3 align=center >现在时间为:"+sdf.format(date)+"</h3>");
	response.setHeader("Refresh", "1");//每隔一秒刷新一次
%>

</body>
</html>

效果:
在这里插入图片描述
说明:
上面的时间是每一秒都在不断的变化,这就是刷新的作用
response.setHeader(“Refresh”, “1”);//每隔一秒刷新一次



respond页面跳转:


<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<h3>
	3秒后跳转到主页面,如果没有跳转<a href="../index.jsp">请点击这里</a>
</h3>

<%
	//response.setHeader("refresh", "3;URL=http://localhost:8080/First/index.jsp");//页面跳转
	response.sendRedirect("http://localhost:8080/First/index.jsp");//页面跳转
	System.out.println("**************跳转后的代码****************");//输出语句可以正常输出
%>

</body>
</html>

说明:
response.setHeader(“refresh”, “3;URL=http://localhost:8080/First/index.jsp”);//页面跳转
这是三秒后进行的跳转,跳转到后面链接(省略http://localhost:8080也是可以的)

说明:
response.sendRedirect(“http://localhost:8080/First/index.jsp”);//页面跳转
这是页面立即跳转((省略http://localhost:8080也是可以的))

说明:
下面图中可以看出页面跳转后面的输出语句可以正常的输出,这里注意区分与forward跳转的区别

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱睡觉的小馨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值