您的位置:首页 > 其它

分析:重定向和请求转发

2013-06-15 14:17 288 查看

分析:重定向和请求转发

重定向

  HttpServletResponse对象的sendRedirect(java.lang.String location)方法称作重定向

  如果location地址前面加上“/”,则表示相对于Servlet容器的根来请求,比如http://localhost:8080;如果location地址前面没有加上“/”,则表示相对于当前请求的URI来寻找地址。

请求转发

  RequestDispatcher的:forward(ServletRequest request, ServletResponse response)方法叫做请求转发。

实验例子1:重定向和请求转发似乎都是造成页面跳转

  第一个页面first.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 'first.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 action="Second">
<input type="text" name="username">
<input type="submit" value="submit">
</form>
</body>
</html>


first.jsp

  第二个页面是Servlet:

  用请求转发:

package com.shengqishiwind.servlet;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Second extends HttpServlet
{

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
process(request, response);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
process(request, response);

}

private void process(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
// 请求转发
RequestDispatcher rd = request.getRequestDispatcher("third.jsp");
rd.forward(request, response);
}

}


  用重定向,则把处理方法改为:

private void process(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
// 重定向
response.sendRedirect("third.jsp");

}


  第三个页面是third.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 'third.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 Third page. <br>
</body>
</html>


  不管用请求转发还是重定向的方法,第一个页面点击提交后,都能顺利转到第三个页面:



  但是其实实际进行的操作还是很不同的,看下面的例子。

实验例子2:如果要在第三个页面中取得第一个页面输入的用户名

  请求转发的实现比较简单,第二个页面的Servlet代码不用添加任何东西,可以直接从第三个页面getParameter,即把third.jsp中的body改为:

<body>
This is my Third page. <br>
用户名:<%=request.getParameter("username") %>
</body>


  则在第一个页面输入shengqishiwind,提交后,第三个页面显示:



  重定向应该怎么实现获取第一个页面提交的用户名呢?

  第三个页面采用如上的代码(通过getParameter获取参数),把第二个页面中改为重定向的跳转方式,重新来一次,可以看到显示:



  再尝试第二个页面这样(通过setAttribute存储值):

private void process(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
String username = request.getParameter("username");
// 重定向
request.setAttribute("username", username);
response.sendRedirect("third.jsp");

}


  然后第三个页面这样获取:

用户名:<%=request.getAttribute("username") %>


  仍然是不行,得到的仍然是null值。

重定向和请求转发的区别

请求转发:

  RequestDispatcher是通过调用HttpServletRequest对象的getRequestDispatcher()方法得到的,是属于请求对象的方法。

  请求转发,整个过程处于同一个请求当中。

  不管经历了多少组件,都可以从request中直接取得想要的值。

  使用请求转发时,到结果页面的网址是:http://localhost:8080/HelloWeb/Second?username=wind

重定向:

  sendRedirect()是HttpServletResponse对象的方法,即响应对象的方法。

  既然调用了响应对象的方法,那就表明整个请求过程已经结束了,服务器开始向客户端返回执行的结果。

  sendRedirect调用后会向客户端返回一个响应,这个响应告诉客户端要转向的页面,紧接着客户端又会发送一个新的请求,转向这个目标页面。

  也即是说,重定向的过程中实际上客户端会向服务器发送两个请求。

  使用重定向时,结果页面的网址是:http://localhost:8080/HelloWeb/third.jsp

  说明客户端已经知道了结果页面的地址,是重新发送的全新的请求。

  重定向方式在Firebug中的图:



  总结记忆请求转发只有一个请求,所以勇往直前,调用的方法叫forward;而重定向需要客户端重新发送请求,调用的是sendRedirect,名字里有个Re,表示重复。

参考资料:

  圣思园张龙老师视频教程。

  Java Document API with examples:

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