一:
1.介紹知識點
1). 本質區別: 請求的轉發只發出了一次請求, 而重定向則發出了兩次請求.
具體:
①. 請求的轉發: 地址欄是初次發出請求的地址.
請求的重定向: 地址欄不再是初次發出的請求地址. 地址欄為最后響應的那個地址
②. 請求轉發: 在最終的 Servlet 中, request 對象和中轉的那個 request 是同一個對象.
請求的重定向: 在最終的 Servlet 中, request 對象和中轉的那個 request 不是同一個對象.
③. 請求的轉發: 只能轉發給當前 WEB 應用的的資源
請求的重定向: 可以重定向到任何資源.
④. 請求的轉發: / 代表的是當前 WEB 應用的根目錄
請求的重定向: / 代表的是當前 WEB 站點的根目錄.
二:程序
1.web.xml(關於配置使用注解了)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://xmlns.jcp.org/xml/ns/javaee" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 5 id="WebApp_ID" version="3.1"> 6 <display-name>JspTest</display-name> 7 </web-app>
2.jsp文件(兩種鏈接代表兩種知識點)
1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 pageEncoding="ISO-8859-1"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <!-- 請求轉發 --> 11 <a href="forwardServlet">Forward</a> 12 13 <br><br> 14 15 <!-- 重定向 --> 16 <a href="redirectServlet">RedirectServlet</a> 17 </body> 18 </html>
3.ForwardServlet.java
1 package Servlets; 2 3 import java.io.IOException; 4 5 import javax.servlet.RequestDispatcher; 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 /** 13 * Servlet implementation class ForwardServlet 14 */ 15 @WebServlet("/forwardServlet") 16 public class ForwardServlet extends HttpServlet { 17 private static final long serialVersionUID = 1L; 18 19 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 20 System.out.println("ForwardServlet doGet"); 21 //請求轉發 22 String path="nextServlet"; 23 RequestDispatcher requestDispatcher=request.getRequestDispatcher("/"+path); 24 requestDispatcher.forward(request, response); 25 } 26 27 }
4.NextServlet.java
1 package Servlets; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.annotation.WebServlet; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 /** 11 * Servlet implementation class NextServlet 12 */ 13 @WebServlet("/nextServlet") 14 public class NextServlet extends HttpServlet { 15 private static final long serialVersionUID = 1L; 16 17 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 18 System.out.println("NextServlet doGet"); 19 } 20 }
5.RedirectServlet.java
1 package Servlets; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.annotation.WebServlet; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 /** 11 * Servlet implementation class RedirectServlet 12 */ 13 @WebServlet("/redirectServlet") 14 public class RedirectServlet extends HttpServlet { 15 private static final long serialVersionUID = 1L; 16 17 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 18 System.out.println("RedirectServlet doGet"); 19 20 //重定向 21 String path="nextServlet"; 22 response.sendRedirect(path); 23 } 24 }
6.效果
分別點擊一次鏈接。