response对象

本章目标
掌握response的主要使用,及对应接口定义
可以使用response设置头信息
可以使用response进行客户端跳转
操作Cookie

 

response对象
response对象的主要作用是用于对客户端的请求进行回应,将WEB服务器处理后的结果发回给客户端。response对象属于javax.servlet.http.HttpServletResponse接口的实例,HttpServletResponse接口的定义如下:
public interface HttpServletResponse extends ServletResponse

 

response对象的常用方法

 

设置刷新头信息
之前曾经讲解过,客户端在进行请求的时候会发送许多额外的信息,那么这些就是头信息,服务器端也可以根据需要向客户端设置头信息,在所有的头信息的设置中,定时刷新页面的头信息使用的是最多的,直接使用setHeader()方法,将头信息名称设置为refresh,同时指定刷新的时间。

设置定时刷新的头信息

<%@ page language="java" contentType="text/html" pageEncoding="utf-8"%>
<html>
<head>
<title>设置定时刷新的头信息</title>
</head>
<body>
<%!//定义全局变量
	int count=0;
 %>
 <%
 	response.setHeader("refresh", "2");//设置两秒一刷新
  %>
  <h3>已经访问了<%=count++ %>次!</h3>
</body>
</html>

效果图:

 

3秒后跳转到其他页面

<%@ page language="java" contentType="text/html" pageEncoding="utf-8"%>
<html>
<head>
<title>3秒后跳转到其他页面</title>
</head>
<body>
<h3>3秒后跳转到hello.htm页面,如果没有跳转请按<a href="hello.html">这里</a>!</h3>
 <%
 	response.setHeader("refresh", "3;URL=hello.html");//3秒后跳转到hello.html
  %>

</body>
</html>

 效果图:

 

跳转后的显示页面

<html>
<head>
<title>跳转后的显示页面</title>
</head>
<body>
	<h3>阅谁问君诵,水落清香浮。</h3>
</body>
</html>

效果图:

 

刷新的HTML 指令
<META HTTP-EQUIV="refresh" CONTENT="3;URL=hello.htm">

 

使用HTML完成定时跳转的功能

<html>
<head>
<title>使用HTML完成时跳转的功能</title>
<meta http-equiv="refresh" content="3; url=hello.html">
</head>
<body>
	<h3>3秒后跳转到hello.htm页面,如果没有跳转请按<a href="hello.html">这里</a>!</h3>
</body>
</html> 

效果图:


 
 页面跳转
在JSP中除了可以通过头信息的方式完成跳转,也可以使用response对象的sendRedirect()方法直接完成页面的跳转。

 

使用sendRedirect()跳转到hello.html页

<%@ page language="java" contentType="text/html" pageEncoding="utf-8"%>
<html>
<head>
<title>使用sendRedirect()跳转到hello.html页</title>
</head>
<body>
 <%
 	response.sendRedirect("hello.html");//直接跳转到hello.html
  %>
</body>
</html>

 效果图:同上

 

两种跳转的区别是什么?
<jsp:forward>属于服务器端跳转,跳转之后地址栏的信息并不会有任何的改变,而response.sendRedirect()属于客户端跳转,跳转之后地址栏是会改变的,变为跳转之后的页面地址。
在使用request范围属性的时候,只有是服务器端跳转才能够将request属性保存到跳转页,而如果是客户端跳转,则无法进行属性的传递。
如果使用的是服务器端跳转的话,则执行到跳转语句之后会立刻进行跳转,如果使用的是客户端跳转,则是在整个页面执行完之后才执行跳转。

 

操作Cookie
Cookie是浏览器所提供的一种技术,这种技术让服务器端的程序能将一些只须保存在客户端,或者在客户端进行处理的数据,放在使用中本身的计算机,不须通过网络的传输,因而提高网页处理的效率,而且也能够减少服务器端的负载。但是由于Cookie是服务器端保存在客户端的信息,所以其安全性也是很差的。
在JSP中专门提供了javax.servlet.http.Cookie的操作类。

 

Cookie定义的常用方法  

 

向客户端增加Cookie

<%@ page language="java" contentType="text/html" pageEncoding="utf-8"%>
<html>
<head>
<title>向客户端增加Cookie</title>
</head>
<body>
 <%
 	Cookie c1=new Cookie("cy","chaoyi");//定义新的Cookie对象
 	Cookie c2=new Cookie("baidu","www.baidu.com");//定义新的Cookie对象
 	response.addCookie(c1);//向客户端增加Cookie
 	response.addCookie(c2);//向客户端增加Cookie
  %>
</body>
</html>

 

取出设置的Cookie

<%@ page language="java" contentType="text/html" pageEncoding="utf-8"%>
<html>
<head>
<title>取出设置的Cookie</title>
</head>
<body>
 <%
	Cookie c[]=request.getCookies();//取得全部的Cookie
	for(int x=0;x<c.length;x++){//循环取出每一个Cookie
%>
		<h3><%=c[x].getName() %>--&gt;<%=c[x].getValue() %></h3>
<%	
	}
  %>
</body>
</html>

 效果图:

 

为Cookie设置保存时间

<%@ page language="java" contentType="text/html" pageEncoding="utf-8"%>
<html>
<head>
<title>为Cookie设置保存时间</title>
</head>
<body>
 <%
 	Cookie c1=new Cookie("cy","chaoyi");//定义新的Cookie对象
 	Cookie c2=new Cookie("baidu","www.baidu.com");//定义新的Cookie对象
 	c1.setMaxAge(60);//Cookie保存60秒
 	c2.setMaxAge(60);//Cookie保存60秒
 	response.addCookie(c1);//向客户端增加Cookie
 	response.addCookie(c2);//向客户端增加Cookie
  %>
</body>
</html>

 

小结
response表示的是服务器端对客户端的回应
可以通过setHeader()方法设置一个响应的头信息
通过response可以向客户端设置cookie,通过request可以取得客户端设置的全部Cookie
response对象属于的sendRedirect()方法属于客户端跳转,而<jsp:forward>属于服务器端跳转

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值