response对象

response对象用于响应客户端请求,向客户输出信息。他封装了JSP产生的响应,并发送到客户端以响应客户端请求。

 

1.重定向网页

使用response对象的sendRedirect()方法可以将网页重定向到另一个页面。重定向支持将地址重定向到不同的主机上,这一点与转发不同。在客户端浏览器上将会得到跳转后的地址,并重新发送请求链接;用户可以从浏览器的地址栏中看到跳转后的地址;重定向操作后,request中的属性将会全部失效,并开始一个新的request对象

sendRedirect()方法的语法格式如下:

response.sendRedirect(String path);

参数说明:

path:指定的目标地址,可以是相对路径,也可以是不同主机的其他URL地址

 

范例:

定义index.jsp重定向到Login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%response.sendRedirect("Login.jsp"); %>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
    
<title>My JSP 'index.jsp' starting page</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<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>
<br>
</body>
</html>

 

 

定义Login.jsp

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>My JSP 'Login.jsp' starting page</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<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>
<form  name="form1" method="post" action="">
<input name="name" type="text" id="name" style="width:120px"><br/><br/>
<input name="pass" type="password" id="pass" style="width:120px"><br/>
<br/>
<input type="submit" name="submit" value="提交">
</form>
</body>
</html>

 

2.处理HTTP头文件

response对象可以设置HTTP响应包头,其中最常用的是禁用缓存,设置页面自动刷新和定时跳转页面。

禁用缓存

默认情况下,浏览器将会显示的网页的内容进行缓存,可以通过response对象来实现对缓存的禁用。

范例:

修改Login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
response.setHeader("Catch-control", "no-store");
response.setDateHeader("Expires", 0);
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
    
<title>My JSP 'Login.jsp' starting page</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<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>
<form  name="form1" method="post" action="">
<input name="name" type="text" id="name" style="width:120px"><br/><br/>
<input name="pass" type="password" id="pass" style="width:120px"><br/>
<br/>
<input type="submit" name="submit" value="提交">
</form>
</body>
</html>

 

设置页面定时跳转

范例:

修改index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
"refresh", "5;URL=hello.jsp");
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
    
<title>My JSP 'index.jsp' starting page</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<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>
<br>
</body>
</html>

 

 

 

设置页面定时刷新

<%
   
"refresh", "5");
     
%>

设置输出缓冲

通常情况下,服务器要输出到客户端的内容不会直接写到客户端,而是先写到一个输出缓冲区。当满足下面三种情况之一就会把缓冲区的内容写到客户端

A.JSP页面的输出已经全部写到了缓冲区

B.缓冲区已经满了

C.在JSP页面中调用了response对象的flushBuffer()方法或者是out对象的flush()方法

 

response对象对缓冲区的配置有如下几种方法:

flushBuffer():强制将缓冲区中的内容输出到客户端

getBufferSize():获取相应所使用的缓冲区的实际大小。如果没有缓冲区则返回0

reset():清除缓冲区的内容 ,同时清除状态码和报头

isCommited():检测服务器端是否已经把数据写入客户端。

 

范例:

将缓冲区的大小设置为20KB

response.setBufferSize(); 

PS:如果将缓冲区的大小设置为0则表示不缓冲


Java response 重定向页面如何带参数 使用response对象进行重定向_客户端