首页 技术 正文
技术 2022年11月12日
0 收藏 830 点赞 2,413 浏览 4606 个字

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,129
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,601
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,444
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,218
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,852
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,940