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对象的sendRe ...

  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第二篇【内置对象的介绍、4种属性范围、应用场景】

    什么是JSP内置对象 JSP引擎在调用JSP对应的jspServlet时,会传递或创建9个与web开发相关的对象供jspServlet使用.JSP技术的设计者为便于开发人员在编写JSP页面时获得这些w ...

  8. jsp内置对象-response对象

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

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

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

随机推荐

  1. 原生js实现简单移动端轮播图

    最近项目不是很忙,自己就用原生js写了一个简单的移动端轮播图的小demo,可实现自动轮播和手势滑动轮播,然后就把它记录到个人博客里.还有很多不足的地方,希望多多指出,以便改进. 1.代码部分 分为四个 ...

  2. js数组元素的添加和删除

    简单测试例子: var arr = new Array(); arr[0] = "aaa"; arr[1] = "bbb"; arr[2] = "cc ...

  3. ASP.NET Core 与 Vue.js 服务端渲染

    http://mgyongyosi.com/2016/Vuejs-server-side-rendering-with-aspnet-core/ 原作者:Mihály Gyöngyösi 译者:oop ...

  4. C# 判断文件是否文本文件

    在网上查了好多资料,大部分都是通过将文件读成二进制流,取前两个字节判断,比如.jpg的是255216.代码如下: ); i++; }return isTextFile; }catch (Excepti ...

  5. 从java1到java9每个版本都有什么新特性?

    每次出新版本,大家大概都会这么问,"Java X会有什么特性呢?" .在下面的内容里,我总结了至今为止的Java主要发行版中各自引入的新特性,这样做的目的是为了突出各个新特性是在哪 ...

  6. 《Linux命令行与shell脚本编程大全》第十章 使用编辑器

    主要介绍vim, nano, emacs,KWrite,Kate,GNOME 10.1 vim Unix系统最初的编辑器 10.1.1检查vim软件包 先搞明白你所用的Linux系统是哪种vim软件包 ...

  7. Json字符串解析原理、超大json对象的解析

    概述 附上完整的代码:https://pan.baidu.com/s/1dEDmGz3(入口类是Json)JSON:JavaScript 对象表示法(JavaScript Object Notatio ...

  8. mysql timeout

    (待更新整理) 因为最近遇到一些超时的问题,正好就把所有的timeout参数都理一遍,首先数据库里查一下看有哪些超时: root@localhost : test 12:55:50> show ...

  9. 原码、反码、补码的正(nao)确(can)打开方式

    我们知道日常生活中使用的数分为整数和实数,整数的小数点固定在数的最右边,可以省略不写,而实数的小数点则不固定.在计算机中只能识别和表示“0”和“1”,而无法识别小数点,因此要想使得计算机能够处理日常使 ...

  10. 转贴---Performance Counter(包含最全的Windows计数器解释)

    http://support.smartbear.com/viewarticle/55773/ 这个Article中介绍了一个新工具,TestComplete,把其中涉及到性能计数器的部分摘抄出来了. ...