| 关键词: MOV 圆盘 递归 CALL DISPLAY push 输出 塔座 three Hanoi |
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/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=""> 用户名:<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=""> 用户名:<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"); %>设置输出缓冲通常情况下,服务器要输出到客户端的内容不会直接写到客户端,而是先写到一个输出缓冲区。当满足下面三种情况之一就会把缓冲区的内容写到客户端A.JSP页面的输出已经全部写到了缓冲区B.缓冲区已经满了C.在JSP页面中调用了response对象的flushBuffer()方法或者是out对象的flush()方法 response对象对缓冲区的配置有如下几种方法:flushBuffer():强制将缓冲区中的内容输出到客户端getBufferSize():获取相应所使用的缓冲区的实际大小。如果没有缓冲区则返回0reset():清除缓冲区的内容 ,同时清除状态码和报头isCommited():检测服务器端是否已经把数据写入客户端。 范例:将缓冲区的大小设置为20KBresponse.setBufferSize(); PS:如果将缓冲区的大小设置为0则表示不缓冲 |
|
声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系
[邮箱地址] 删除
|