一、response对象
response对象的主要作用是用于对客户端的请求进行回应,将WEB服务器处理后的结果发回给客户端。
response对象属于javax.servlet.http.HttpServletResponse接口的实例,HttpServletResponse接口的定义如下:
public interface HttpServletResponse extends ServletResponse
response对象的常用方法
No. | 方法 | 类型 | 描述 |
1 | Public void addCookie(Cookie cookie) | 普通 | 向客户端增加Cookie |
2 | Public void setHeader(String name, String value) | 普通 | 设置回应的头信息 |
3 | Public void sendRedirect(String location) throws IOException | 普通 | 页面跳转 |
二、设置头信息
所有的头信息都是随着请求和回应自动发送到服务器端(客户端),在response中一个比较常用的头信息就是刷新指令,可以完成定时刷新的功能。
<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head><title>Welcome</title></head>
<body>
<%! //定义全局变量
int count = 0;
%>
<%
response.setHeader("refresh", "2"); //页面2秒刷新一次
%>
<h3>已经访问了<%=count++%>次</h3>
</body>
</html>
对于刷新的头信息,除了定时的功能外,还具备了定时跳转的功能,可以让一个页面定时跳转到一个指定的页面
<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head><title>Welcome</title></head>
<body>
<h3>3秒后跳转到welcome.html页面</h3>
<%
response.setHeader("refresh", "3;URL=welcome.html"); //3秒后跳转到welcome.html页面
%>
</body>
</html>
对于这种定时跳转的头信息也可以采用HTML的方式进行设置,HTML本身也可以设置头信息
<html>
<head><title>Welcome</title></head>
<META HTTP-EQUIV="refresh" CONTENT="3;URL=welcome.html">
<body>
<h3>3秒后跳转到welcome.html页面,如果没有跳转请点击<a href="welcome.html">这里</a></h3>
</body>
</html>
以上跳转之后地址栏已经发生了改变,此时跳转是客户端跳转,在WEB中只要是包含了动态页的,都需要WEB容器的支持,而所有的静态页直接使用即可。三、页面跳转
在response对象中提供了一个sendRedirect()方法完成跳转的功能。
<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head><title>Welcome</title></head>
<body>
<%
response.sendRedirect("welcome.html");
%>
</body>
</html>
此时已经跳转成功,而且地址栏改变了,那么跳转就属于客户端跳转,客户端跳转就无法传递request属性范围内容的。
<jsp:forward>跳转和response.sendRedirect()跳转的区别:
如果使用<jsp:forward>跳转有如下特点:
1.服务器端跳转,跳转之后地址栏不改变
2.属于无条件的跳转,执行到之后立刻跳转。跳转之前的语句会执行,而跳转之后的语句将不会执行。所以在JSP中使用了JDBC的话,必须在跳转之前进行数据库的关闭,否则数据库再也无法关闭
样例1:
<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head><title>Welcome</title></head>
<body>
<%
System.out.println("=============forward跳转之前=============");
%>
<jsp:forward page="welcome.html"/>
<%
System.out.println("=============forward跳转之后=============");
%>
</body>
</html>
如果使用的是response.sendRedirect()跳转,有如下特点:
1.客户端跳转,跳转之后地址栏改变,不可以传递request属性
2.是在所有的语句都执行完之后才可以完成的跳转操作
四、操作Cookie
Cookie是浏览器所提供的一种技术,这种技术让服务器端的程序能将一些只须保存在客户端,或者在客户端进行处理的数据,放在使用中本身的计算机,不须通过网络的传输,因而提高网页处理的效率,而且也能够减少服务器端的负载,但是由于Cookie是服务器端保存在客户端的信息,所以其安全性也是很差的。
在JSP中专门提供了javax.servlet.http.Cookie的操作类。
Cookie定义的常用方法
No. | 方法 | 类型 | 描述 |
1 | Public Cookie(String name, String value) | 普通 | 实例化Cookie对象,同时设置名字和内容 |
2 | Public String getName() | 普通 | 取得Cookie的名字 |
3 | Public String getValue() | 普通 | 取得Cookie的内容 |
4 | Public void setMaxAge(int expiry) | 普通 | 设置Cookie的保存时间,以秒为单位 |
所有的Cookie是由服务器端设置到客户端上去的,所以要想客户端增加Cookie的话,必须使用response对象的以下方法:
public void addCookie(Cookie cookie)
设置Cookie:
<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head><title>Welcome</title></head>
<body>
<%
Cookie c1 = new Cookie("Joywy", "11111");
Cookie c2 = new Cookie("Xiaowei", "22222");
response.addCookie(c1);
response.addCookie(c2);
%>
</body>
</html>
取得Cookie:
<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head><title>Welcome</title></head>
<body>
<%
Cookie c[] = request.getCookies();//取得客户端的全部Cookie
for(int x = 0; x < c.length; x++){
%>
<h3><%=c[x].getName()%>---><%=c[x].getValue()%></h3>
<%
}
%>
</body>
</html>
注意:当我们重新打开一个浏览器,则无法取得Cookie了,会出现HTTP Status 500错误、NullPointerException。原因是没有设置保存时间,所以默认是在一个浏览器上保存的。如果才浏览器关闭,则Cookie消失。若想保存在客户端上一段时间,则必须使用Cookie类提供的setMaxAge()方法完成。
<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head><title>Welcome</title></head>
<body>
<%
Cookie c1 = new Cookie("Joywy", "11111");
Cookie c2 = new Cookie("Xiaowei", "22222");
c1.setMaxAge(100);
c2.setMaxAge(100);
response.addCookie(c1);
response.addCookie(c2);
%>
</body>
</html>
注意:先设置时间,再添加cookie.
response对象主要表示的是服务器端对客户端的回应,对于Cookie的操作本身存在安全隐患的,如果要存放客户端信息的时候都使用Cookie保存即可。