JSP详细篇——response对象

response对象

response对象用于响应客户端请求,向客户输出信息。他封装了JSP产生的响应,并发送到客户端以响应客户端请求。

 

1.重定向网页

使用response对象的sendRedirect()方法可以将网页重定向到另一个页面。重定向支持将地址重定向到不同的主机上,这一点与转发不同。在客户端浏览器上将会得到跳转后的地址,并重新发送请求链接;用户可以从浏览器的地址栏中看到跳转后的地址;重定向操作后,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/csshref="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/csshref="styles.css">

-->

 

  </head>

  

  <body>

    <form  name="form1" method="post" action="">

     用户名:<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/csshref="styles.css">

-->

 

  </head>

  

  <body>

    <form  name="form1" method="post" action="">

     用户名:<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/csshref="styles.css">

-->

 

  </head>

  

  <body>

    This is my JSP page. <br>

  </body>

</html>

 

 

 

设置页面定时刷新

   <%

   

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

    

     %>

设置输出缓冲

通常情况下,服务器要输出到客户端的内容不会直接写到客户端,而是先写到一个输出缓冲区。当满足下面三种情况之一就会把缓冲区的内容写到客户端

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

B.缓冲区已经满了

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

 

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

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

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

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

isCommited():检测服务器端是否已经把数据写入客户端。

 

范例:

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

response.setBufferSize(); 

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



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值