JSP的學習二(請求轉寄與 重新導向)

來源:互聯網
上載者:User

標籤:insert   ima   請求   code   doget   區別   XML   ring   splay   

一:

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 ForwardServlet14  */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 NextServlet12  */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 RedirectServlet12  */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.效果

  分別點擊一次連結。

  

 

JSP的學習二(請求轉寄與 重新導向)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.