您的位置:首页 > Web前端 > JavaScript

02---jsp内置对象03(response对象)

2012-04-10 14:18 369 查看
response的主要功能是用于服务器对客户端的回应,将web服务器处理后的结果发回给客户端,response对象

属于javax.servlet.http.HttpServletResponse接口的实例;

response对象的常用方法:

public void addCookie(Cookie cookie)---向客户端增加cookie

public void setHeader(String name,String value)---设置回应的头信息

public void sendRedirect(String location) throws IOException---页面跳转

1、设置头信息:

所有的头信息都是随着请求和回应自动发送到服务器端(客户端),在response中

一个比较常用的头信息就是刷新指令,可以完成定时刷新的功能;

<%@ page contentType="text/html" pageEncoding="gbk"%>

<%@page import="java.util.*"%>

<html>

<head><title>这是测试</title></head>

<body>

<%!//定义全局变量

int count=0;

%>

<%

response.setHeader("refresh","2");//页面2面一刷新

%>

<h2>页面已经访问了<%=count++%>次</h2>

</body>

</html>

对于刷新的头信息,除了定时的功能外,还具备了定时跳转的功能;可以让一个页面

定时的跳转到一个指定的页面;

<%@ page contentType="text/html" pageEncoding="gbk"%>

<%@page import="java.util.*"%>

<html>

<head><title>这是测试</title></head>

<body>

<h2>三秒后跳转到hello.html</h2>

<%

response.setHeader("refresh","3;URL=hello.html");//3秒后跳转到hello.html

%>

</body>

</html>

但是这种跳转并不是万能的,有时候根据无法进行跳转的操作----跳转完后 后退,这时就无法进行

在跳转了,解决办法:

<%@ page contentType="text/html" pageEncoding="gbk"%>

<%@page import="java.util.*"%>

<html>

<head><title>这是测试</title></head>

<body>

<h2>三秒后跳转到hello.html,如果无法跳转请点击<a href="hello.html">这里</a></h2>

<%

response.setHeader("refresh","3;URL=hello.html");//3秒后跳转到hello.html

%>

</body>

</html>

对于这种定时跳转的头信息也可以采用html的方式进行设置,html本身也可以设置头信息:

<%@ page contentType="text/html" pageEncoding="gbk"%>

<%@page import="java.util.*"%>

<html>

<head>

<META HTTP-EQUIV="refresh" CONTENT="3;URL=hello.html">

</head>

<body>

<h2>三秒后跳转到hello.html,如果无法跳转请点击<a href="hello.html">这里</a></h2>

</body>

</html>

此时的跳转地址栏地址发生了变化,属于客户端的跳转;

提问:到时是使用response设置还是使用<META>设置呢?

在web中只要是包含了动态页的,都需要web容器的支持,而所有的静态页直接使用即可;

2、页面的跳转:

在response对象中提供了一个sendRedirect()方法完成跳转的功能;

<%@ page contentType="text/html" pageEncoding="gbk"%>

<%@page import="java.util.*"%>

<html>

<head>

</head>

<body>

<%response.sendRedirect("hello.html");%>

</body>

</html>

此时已经跳转成功,而且地址栏改变了,那么这种跳转属于客户端跳转,客户端跳转

是无法传递request属性范围内容的;

问题:两种跳转的区别:

<jsp:forward>和response.sendRedirect();

如果使用<jsp:forward>跳转的话,有如下特点:

·服务器跳转,跳转之后地址栏不改变

·属于无条件跳转,执行到后立即跳转,跳转之前的语句会执行,跳转语句之后的语句不会执行;

如果是用的是response.sendRedirect():

·客户端跳转

·跳转之间和之后的语句都会被执行,即执行完所有的语句后在跳转;

3、操作cookie:

Cookie是浏览器所提供的一种技术,这种技术让服务器端的程序能将一些只须保存在客户端,

或者在客户端进行处理的数据,放在使用的计算机本身的计算机,不需要通过网络传输,因而提供

网页处理的效率,而且也能够减少服务器端的负载,但是Cookie是服务器保存在客户端的信息,所

以安全性很差;

javax.servlet.http.Cookie

在一般的站点中往往都会存在记住用户名的这样的一种操作,只是将信息保存在本机上,如果

换电脑无效;

<%@ page contentType="text/html" pageEncoding="gbk"%>

<%@page import="java.util.*"%>

<html>

<head>

</head>

<body>

<%

Cookie c1=new Cookie("name1","lid");

Cookie c2=new Cookie("name2","yuj");

response.addCookie(c1);

response.addCookie(c2);

%>

</body>

</html>

以上表示在客户端上设置两个Cookie对象,既然可以设置了,就需要取得;

客户端每次请求的时候都会发送一个包含Cookie的头信息,所以要取得Cookie

则必须使用request对象完成;

<%@ page contentType="text/html" pageEncoding="gbk"%>

<%@page import="java.util.*"%>

<html>

<head>

</head>

<body>

<%

Cookie c[]=request.getCookies();//取得全部Cookie

for(int i=0;i<c.length;i++){

%>

<h3><%=c[i].getName()%>----><%=c[i].getValue()%></h3>

<%

}

%>

</body>

</html>

现在来看,设置Cookie可以取得了,可是如果按照Cookie的理论来讲,

所有的Cookie都应该保存在了客户端,那么即使现在的浏览器关闭了也应该

可以继续取得;

现在为止的Cookie由于没有设置保存的时间,所有默认是 在一个浏览器上保存的;

如果此浏览器关闭Cookie消失(),那么要想真正的保存在客户端上一段时间,则必须

使用Cookie提供的setMaxAge()方法完成;

<%@ page contentType="text/html" pageEncoding="gbk"%>

<%@page import="java.util.*"%>

<html>

<head>

</head>

<body>

<%

Cookie c1=new Cookie("name1","lid");

Cookie c2=new Cookie("name2","yuj");

c1.setMaxAge(100);

c2.setMaxAge(100);

response.addCookie(c1);

response.addCookie(c2);

%>

</body>

</html>

注:换浏览器 Cookie也是无法访问到的,Cookie是保存的在浏览器上的;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: