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

JSP具体条款——response对象

2015-06-19 08:19 495 查看

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则表示不缓冲

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: