重定向
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>
第二個頁面是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,表示重復。

