response对象

response为响应对象client要求。输出信息到客户。他封装JSP反应生成。发送client在回应client要求。

1.重定向网页

使用response对象的sendRedirect()方法能够将网页重定向到还有一个页面。重定向支持将地址重定向到不同的主机上,这一点与转发不同。

在client浏览器上将会得到跳转后的地址,并又一次发送请求链接;用户能够从浏览器的地址栏中看到跳转后的地址。重定向操作后,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>

This is my JSP page. <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="">

username:<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="">

username:<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+"/";

%>

<%

response.setHeader("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>

This is my JSP page. <br>

</body>

</html>

设置页面定时刷新

<%

response.setHeader("refresh", "5");

%>

设置输出缓冲

通常情况下,server要输出到client的内容不会直接写到client。而是先写到一个输出缓冲区。

当满足以下三种情况之中的一个就会把缓冲区的内容写到client

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

B.缓冲区已经满了

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

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

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

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

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

isCommited():检測server端是否已经把数据写入client。

范例:

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

response.setBufferSize();

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

JSP具体条款——response对象的更多相关文章

  1. JSP具体篇——response对象

    response对象 response对象用于响应client请求,向客户输出信息. 他封装了JSP产生的响应,并发送到client以响应client请求. 1.重定向网页 使用response对象的 ...

  2. JSP中使用response对象实现定时跳转网页

    5秒后跳转到登录页面 <% response.setHeader("refresh","5;URL="login.jsp"); %>

  3. jsp的9大对象

    1.requset对象 主要用于接受客户端通过HTTP协议传送给服务器端的数据     request.getProtocal()获得客户使用协议     request.getServletPath ...

  4. 通过response对象的sendRedirect方法重定向网页

    通过response对象的sendRedirect方法重定向网页 制作人:全心全意 使用response对象提供的sendRedirect()方法可以将网页重定向到另一个页面.重定向操作支持将地址重定 ...

  5. JSP内置对象--response对象 (addCookie(),setHeader(),sendRedirect())

    服务器接收客户端请求:request 服务器对客户端的回应:response javax.servlet.http的接口HttpServletResponse extends ServletRespo ...

  6. Jsp——response对象

    <%@ page language="java" contentType="text/html; charset=UTF-8" import=" ...

  7. jsp内置对象-response对象

    一.概念 隐含对象response是javax.servlet.HttpServletResponse接口实现类的对象.response对象封装了JSP产生的响应,用于响应客户端的请求,向客户端输出信 ...

  8. JSP内置对象——response对象

    看一个实例: 运行结果: 出现了一个很奇怪的现象,这个outer对象输出的字符串,跑到顶部去了.这个呢也就说明了response对象获得的writer对象的输出总是前于我们的内置对象.(respons ...

  9. JSP 中的 Request 和 Response 对象

    客户端的请求信息被封装在request对象中,通过它才能了解到客户的需求,然后做出响应.它是HttpServletRequest类的实例:response对象包含了响应客户请求的有关信息,但在JSP中 ...

随机推荐

  1. 《Qt on Android核心编程》夹

    china-pub在售前,售中环节退房,折扣低! 有朋友想看看<Qt on Android核心编程>的文件夹,So-- 文件夹     <Qt on Android核心编程>文 ...

  2. chrome 关闭自己主动更新

    一不小心,把chrome更新成了37, chrome 37不支持模态窗体[ window.showModalDialog() ],折腾死人了. 实在抵不住,仅仅好降级成36.0.1985.143m 至 ...

  3. C++ Primer 学习笔记_2_高速入口(继续)

     P15习题 //题1.14: 试分析假设v1 == v2的情况下,该程序的输出结果 #include <iostream> int main() { std::cout <&l ...

  4. Performance Monitor采集性能数据

    Performance Monitor采集性能数据 Windows本身为我们提供了很多好用的性能分析工具,大家日常都使用过资源管理器,在里面能即时直观的看到CPU占用率.物理内存使用量等信息.此外新系 ...

  5. 白学jquery Mobile《构建跨平台APP:jQuery Mobile移动应用实战》连续7-电话问卷调查

    [例7-3  文本编辑框创建一个简单的调查问卷] 01     <!DOCTYPEhtml> 02     <html> 03     <head> 04     ...

  6. c# winfrom DataGridView使行高不可改变,使列头高度不可改变,

    // 禁止用户改变DataGridView1的所有列的列宽 //DataGridView1.AllowUserToResizeColumns = false; //禁止用户改变DataGridView ...

  7. Python调用微博API

    上头叫通过微博ID获取用户公布过的历史微博内容,于是研究了下新浪微博提供的API 1 首先在微博开放中心下"创建应用"创建一个应用,应用信息那些随便填,填写完成后,不须要提交审核, ...

  8. 解决Virtual Box 安装Mac OS X当出现“hfs: summary table not allowed on FS with block size of 2048”问题

    解决Virtual Box 安装Mac OS X当出现"hfs: summary table not allowed on FS with block size of 2048"问 ...

  9. TCP与UDP的侵略性

    HTTP必须执行在TCP上吗?SSL必须执行在TCP上吗?...实际上HTTP并没有规定一定要执行在TCP上,甚至FTP也不一定要执行在TCP上!HTTP或者FTP仅仅是说底层信道要保证数据的按序传输 ...

  10. Bean Validation 技术规范特性概述

    概述 Bean Validation 规范 Bean 是 Java Bean 的缩写.在 Java 分层架构的实际应用中,从表示层到持久化层.每一层都须要对 Java Bean 进行业务符合性验证,如 ...