JAVA中Response的几种用法(设定时间调整到指定页面 ....... )

时间:2022-11-01 16:03:20
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>Response的使用案例</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<%
response.setHeader("refresh", "10;URL=S.jsp");//定义10秒刷新 跳入到 S.jsp页面
// 当时间设置为 0 ,是则为操作后直接跳转至那个页面。
%>
</body>
</html>

另外是 :设定规格时间内进行 某某 操作:

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>Response的使用案例</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<%!
int count=;
%>
<%
response.setHeader("refresh", "");//定义两秒刷新一次
%>
<h2>已经访问 <%=count++%> 次了 ... ... </h2>
</body>
</html>

注意: 页面间的调整,html也可以实现:

<!DOCTYPE html>
<html>
<head>
<title>jup.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta http-equiv="refresh" content="4;URL=IsOldUse.jsp"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body>
4秒后跳转到 其他页面!!! <br>
</body>
</html>

页面之间的跳转  (response.sendRedirect() 客户端跳转)  || (<jsp:forward page="hello.html" /> 服务器端跳转)

区别:

客户端的跳转:它是执行完整个页面,并且URL地址栏发生改变,在某些传值的情况下不可使用,

服务器端跳转:他是执行到  <jsp:forward page="hello.html" /> 就马上停止,不再继续执行,而他的地址栏URL不变。

注意:在以后的开发过程中,尤其是在使用JDBC的操作中,一定要在  <jsp:forward page="hello.html" /> 语句执行之前关闭数据库的连接,否则数据库连接将再也无法关闭,而如果数据库始终没有关闭,当达到一定程度时将会出现“数据库连接已经达到最大的异常”,此时就只能重启服务器。