1. 程式人生 > >請求轉發&&重定向

請求轉發&&重定向

重定向

      位址列:

http://localhost:8080/ServletRedirectionDemo/login_success.html

伺服器返回的響應訊息頭:Response Header

HTTP/1.1 302 Found
Server: Apache-Coyote/1.1
Location: http://localhost:8080/ServletRedirectionDemo/login_success.html
Content-Type: text/html;charset=UTF-8
Content-Length: 0
Date: Thu, 29 Nov 2018 09:45:10 GMT

用法:

之前的寫法
	response.setStatus(302);
	response.setHeader("Location", "login_success.html");*/
			
重定向寫法: 重新定位方向 引數即跳轉的位置
	response.sendRedirect("login_success.html");

Note:

1. 地址上顯示的是最後的那個資源的路徑地址。

2. 請求次數最少有兩次, 伺服器在第一次請求後,會返回302 以及一個地址, 瀏覽器在根據這個地址,執行第二次訪問。

3. 可以跳轉到任意路徑。 不是自己的工程也可以跳。

4. 效率稍微低一點, 執行兩次請求。 

 5. 後續的請求,沒法使用上一次的request儲存的資料,或者 沒法使用上一次的request物件,因為這是兩次不同的請求。


請求轉發

     位址列:

http://localhost:8080/ServletRedirectionDemo/Login?username=admin&password=12345

     伺服器返回的響應訊息頭:Response Header

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Accept-Ranges: bytes
ETag: W/"148-1543483949046"
Last-Modified: Thu, 29 Nov 2018 09:32:29 GMT
Content-Type: text/html;charset=UTF-8
Content-Length: 148
Date: Thu, 29 Nov 2018 09:46:44 GMT

    請求轉發的寫法: 引數即跳轉的位置

request.getRequestDispatcher("login_success.html").forward(request, response);

    Note:

        1. 地址上顯示的是請求servlet的地址。  返回200 ok

        2. 請求次數只有一次, 因為是伺服器內部幫客戶端執行了後續的工作。 

        3. 只能跳轉自己專案的資源路徑 。  

        4. 效率上稍微高一點,因為只執行一次請求。 

        5. 可以使用上一次的request物件。 


例項:ServletRedirectionDemo

package it.cast.redirection.demo;

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

/**
 * 重定向小練習
 */
public class RedirectionDemo extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	//中文輸出亂碼的處理
	response.setContentType("text/html;charset=UTF-8");
	//獲取請求的資料
	String username = request.getParameter("username");
	String password = request.getParameter("password");
	
	if("admin".equals(username)&&"12345".equals(password)) {
		//之前跳轉頁面的寫法
//		response.setStatus(302);
//		response.setHeader("Location", "login_success.html");
	
		//重定向
//		response.sendRedirect("login_success.html");
		
		//請求轉發
		request.getRequestDispatcher("login_success.html").forward(request, response);
		
	}else{
		response.getWriter().write("登入失敗");
	}
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

登入頁面:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>登入頁面</h1><br/>
	<form action="Login"method="get">
		賬號:<input type="text" name="username"/><br/>
		密碼:<input type="text" name="password"/><br/>
		<input type="submit" value="登入"/>
	</form>
</body>
</html>

登入成功頁面:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>登入成功</h1>
</body>
</html>