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

浅谈Jsp页面的两种跳转方式:客户端重定向和服务器端转发

2015-03-28 23:21 519 查看
一般在JSP页面的跳转中,我们很熟悉的两种就是客户端重定向:response.sendRedirect(“url地址”);

服务器端转发:request.getRequestDispatcher("地址").forword(request,response);

这两种页面跳转中常用。那么他们之间的区别在哪呢,今天的学习让我明白了这两种跳转方式的区别。

先来看看一个简单的例子:

一个简单的登陆模拟的页面:

<!DOCTYPE html>
<html>
  <head>
    <title>reg.html</title>
	
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
  
  <body>
   
    	<h1>用户注册</h1>
    	<form action="doReg.jsp" method="post">
    		<fieldset>
    			<label for="uname"></label>用户名:<input type="text" name="uname"><br />
    		</fieldset>
    		<fieldset>
    			<label for="password"></label>密码:<input type="password" name="password"><br />
    		</fieldset>
    		<fieldset>
    			<label for="ins"></label>兴趣:<input type="checkbox" name="ins" value="游泳">游泳<br/>
    										   <input type="checkbox" name="ins" value="下棋">下棋
    		</fieldset>
    		<input type="submit" value="提交">
    	</form>

  </body>
</html>


action请求页面(doReg.jsp这里获取属性和值是使用自己定义的封装类)

</pre><pre name="code" class="html"><%@ page language="java" import="java.util.*,com.yc.bean.*,com.yc.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 'doReg2.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>
     
<%
	RequestUtil ru=new RequestUtil();
	Student s=ru.parseRequest(request, Student.class);
	
%>
<%
	//客户端重定向
	if("lmj".equals(s.getUname())&&"lmj".equals(s.getPassword())){
		response.sendRedirect("index.jsp");
	}else{
		//给提示结合js
		out.println("<script>alert('名字或者密码错误')</script>");
	}
 %>

  </body>
</html>

此时使用重定向观察页面的跳转如下图:

将这段代码

response.sendRedirect("index.jsp");
改为:request.getRequestDispatcher("地址").forword(request,response);

发现了吧,两者跳转的页面是不一样的,而且测试可知,在用服务器时可以获取到reg.html页面的参数值而

重定向则不可以并且重定向可以访问外网。

客户端重定向

response: 响应对象 将给客户端的响应信息封装 由客户端进行处理

response.sendRedirect();

重定向之后

a.地址栏中的地址是最后一个页面地址

b.无法取到页面上的值

c.可以访问外网

服务器端转发

request.getRequestDispatcher("地址").forward(request,response);

a.请求的页面是请求页面(action)

b.请求的参数可以传递到后面的页面

c.不能访问外网

那么原理是什么呢?

客户端重定向:是由客户端即浏览器先访问服务器,服务器发现response.sendRedirect()后告诉浏览器去访问另外一个服务器(可以是本服务器),这就解释了为什么参数不能传递过去的原因是因为客户端此时要两次访问服务器来完成这个流程,参数就不能在服务器中记录。

服务器转发:它是由客户端直接访问服务器,接收到链接跳转指令后有服务器处理要跳转的页面,即由本服务器自己处理,再由服务器发出跳转请求,相当于浏览器只访问了一次本机服务器,所以才会有上述的区别。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: