自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

聊聊Java中的轉(zhuǎn)發(fā)與重定向

開發(fā) 后端
轉(zhuǎn)發(fā)和重定向都是實(shí)現(xiàn)頁面跳轉(zhuǎn),也就是說,當(dāng)我們訪問一個(gè) Servlet 的時(shí)候 ,Servlet 幫我們跳轉(zhuǎn)到另一個(gè)界面。

 [[390002]]

本文轉(zhuǎn)載自微信公眾號「見賢思編程」,作者泰斗賢若如。轉(zhuǎn)載本文請聯(lián)系見賢思編程公眾號。   

轉(zhuǎn)發(fā)與重定向簡介

轉(zhuǎn)發(fā)和重定向都是實(shí)現(xiàn)頁面跳轉(zhuǎn)

也就是說,當(dāng)我們訪問一個(gè) Servlet 的時(shí)候 ,Servlet 幫我們跳轉(zhuǎn)到另一個(gè)界面。

轉(zhuǎn)發(fā)與重定向的區(qū)別

  • 實(shí)現(xiàn)轉(zhuǎn)發(fā)調(diào)用的是 HttpServletRequest 對象中的方法
  • 實(shí)現(xiàn)重定向調(diào)用的是 HttpServletResponse 對象中的方法
    • 轉(zhuǎn)發(fā)時(shí)瀏覽器中的 url 地址不會發(fā)生改變
    • 重定向時(shí)瀏覽器中的 url 地址會發(fā)生改變
  • 轉(zhuǎn)發(fā)時(shí)瀏覽器只請求一次服務(wù)器
  • 重定向時(shí)瀏覽器請求兩次服務(wù)器
    • 轉(zhuǎn)發(fā)能使用 request 帶數(shù)據(jù)到跳轉(zhuǎn)的頁面
    • 重定向能使用 ServletContext 帶數(shù)據(jù)到跳轉(zhuǎn)的頁面

代碼演示轉(zhuǎn)發(fā)和重定向

  1. package servlet; 
  2.  
  3.  
  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. import java.io.IOException; 
  10.  
  11.  
  12. @WebServlet("/login"
  13. public class ServletDemo extends HttpServlet { 
  14. @Override 
  15. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
  16. //獲取表單提交過來的數(shù)據(jù) 
  17. //getParameter()方法可以獲取請求的參數(shù)信息 
  18. String name = req.getParameter("name"); 
  19. String password = req.getParameter("password"); 
  20.  
  21.  
  22. //打印獲取到的參數(shù)信息 
  23. System.out.println("name:"+name); 
  24. System.out.println("password:"+password); 
  25.  
  26.  
  27. //如果name=admin,password=123,則跳轉(zhuǎn)到succee.jsp,否則跳轉(zhuǎn)到fail.jsp 
  28. if("admin".equals(name)&&"123".equals(password)){ 
  29. //通過轉(zhuǎn)發(fā)實(shí)現(xiàn)跳轉(zhuǎn) 
  30. req.getRequestDispatcher("/success.jsp").forward(req,resp); 
  31. }else { 
  32. //通過重定向?qū)崿F(xiàn)跳轉(zhuǎn) 
  33. resp.sendRedirect("/fail.jsp"); 
  34.  
  35.  
  36. @Override 
  37. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
  38. doGet(req, resp); 
  39.  
  40.  

JSP代碼

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %> 
  2. <html> 
  3. <head> 
  4. <title>登錄</title> 
  5. </head> 
  6. <body> 
  7. <form action="/login"
  8. <table align="center"
  9. <tr> 
  10. <td>賬號:</td> 
  11. <td><input type="text" name="name"></td> 
  12. </tr> 
  13. <tr> 
  14. <td>密碼:</td> 
  15. <td><input type="text" name="password"></td> 
  16. </tr> 
  17. <tr> 
  18. <td><input type="submit" value="登錄"></td> 
  19. <td><input type="reset" value="重置"></td> 
  20. </tr> 
  21. </table
  22. </form> 
  23. </body> 
  24. </html> 

 

 

 

 

 

轉(zhuǎn)發(fā)和重定向如何帶數(shù)據(jù)到某個(gè)頁面

  1. package servlet; 
  2. import javax.servlet.ServletContext; 
  3.  
  4. import javax.servlet.ServletException; 
  5.  
  6. import javax.servlet.annotation.WebServlet; 
  7.  
  8. import javax.servlet.http.HttpServlet; 
  9.  
  10. import javax.servlet.http.HttpServletRequest; 
  11.  
  12. import javax.servlet.http.HttpServletResponse; 
  13.  
  14. import java.io.IOException; 
  15. @WebServlet("/login"
  16.  
  17. public class ServletDemo extends HttpServlet { 
  18.  
  19. @Override 
  20.  
  21. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
  22. //通過轉(zhuǎn)發(fā)帶數(shù)據(jù) 
  23.  
  24. req.setAttribute("name","張三"); 
  25.  
  26. req.getRequestDispatcher("/send.jsp").forward(req,resp); 
  27. @Override 
  28.  
  29. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
  30.  
  31. doGet(req, resp); 
  32.  

send.jsp

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %> 
  2. <html> 
  3. <head> 
  4. <title>轉(zhuǎn)發(fā)和重定向傳代數(shù)據(jù)練習(xí)</title> 
  5. </head> 
  6. <body> 
  7. <% 
  8. //1、接收轉(zhuǎn)發(fā)傳代的數(shù)據(jù) 
  9. String name = (String) request.getAttribute("name"); 
  10. out.println("轉(zhuǎn)發(fā)傳代的數(shù)據(jù):"+name); 
  11.  
  12.  
  13. %> 
  14.  
  15.  
  16. </body> 
  17. </html> 

  1. package servlet; 
  2. import javax.servlet.ServletContext; 
  3.  
  4. import javax.servlet.ServletException; 
  5.  
  6. import javax.servlet.annotation.WebServlet; 
  7.  
  8. import javax.servlet.http.HttpServlet; 
  9.  
  10. import javax.servlet.http.HttpServletRequest; 
  11.  
  12. import javax.servlet.http.HttpServletResponse; 
  13.  
  14. import java.io.IOException; 
  15. @WebServlet("/login"
  16.  
  17. public class ServletDemo extends HttpServlet { 
  18.  
  19. @Override 
  20.  
  21. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
  22.  
  23.  
  24.  
  25. //通過重定向帶數(shù)據(jù) 
  26.  
  27. ServletContext servletContext = this.getServletContext(); 
  28.  
  29. servletContext.setAttribute("name","王二麻子"); 
  30.  
  31. resp.sendRedirect("/send2.jsp"); 
  32. @Override 
  33.  
  34. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
  35.  
  36. doGet(req, resp); 
  37.  

send2.jsp

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %> 
  2. <html> 
  3. <head> 
  4. <title>轉(zhuǎn)發(fā)和重定向傳代數(shù)據(jù)練習(xí)</title> 
  5. </head> 
  6. <body> 
  7. <% 
  8. //1、接收重定向傳代的數(shù)據(jù) 
  9. String name1 = (String)application.getAttribute("name"); 
  10. out.println("重定向傳代的數(shù)據(jù):"+name1); 
  11. %> 
  12. </body> 
  13. </html> 

 

 

練習(xí)

 

index.jsp

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %> 
  2. <html> 
  3. <head> 
  4. <title>Title</title> 
  5. </head> 
  6. <body> 
  7. <form action="CountServlet" method="post"
  8. <h3>加法計(jì)算器</h3> 
  9. 加數(shù)1:<input type="number" name="one"
  10. 加數(shù)2:<input type="number" name="two"
  11. <input type="submit" value="計(jì)算"
  12. </form> 
  13. </body> 
  14. </html> 

count.jsp

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %> 
  2. <html> 
  3. <head> 
  4. <title>Title</title> 
  5. </head> 
  6. <body> 
  7. 計(jì)算結(jié)果:<%=request.getAttribute("count")%> 
  8. <!--計(jì)算結(jié)果:<%=application.getAttribute("count")%>--> 
  9. </body> 
  10. </html> 

Servlet

  1. package servlet; 
  2. import javax.servlet.ServletContext; 
  3. import javax.servlet.ServletException; 
  4. import javax.servlet.annotation.WebServlet; 
  5. import javax.servlet.http.HttpServlet; 
  6. import javax.servlet.http.HttpServletRequest; 
  7. import javax.servlet.http.HttpServletResponse; 
  8. import java.io.IOException; 
  9. @WebServlet("/CountServlet"
  10. public class CountServlet extends HttpServlet { 
  11. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
  12. String one=request.getParameter("one"); 
  13. int o=Integer.parseInt(one);//強(qiáng)制轉(zhuǎn)換,將String類型的數(shù)據(jù)轉(zhuǎn)換成int類型 
  14. String two=request.getParameter("two"); 
  15. int t=Integer.parseInt(two);//強(qiáng)制轉(zhuǎn)換,將String類型的數(shù)據(jù)轉(zhuǎn)換成int類型 
  16. System.out.println(one+" "+two); 
  17. int c=o+t; 
  18. String co=String.valueOf(c);//將int類型的數(shù)據(jù)轉(zhuǎn)換成String類型 
  19. //轉(zhuǎn)發(fā),可以攜帶數(shù)據(jù) 
  20. request.setAttribute("count",co); 
  21. request.getRequestDispatcher("count.jsp").forward(request,response); 
  22. //用于存放數(shù)據(jù) 
  23. // ServletContext s=this.getServletContext(); 
  24. // s.setAttribute("count",co); 
  25. //重定向只能依靠ServletContext獲取數(shù)據(jù) 
  26. // response.sendRedirect("count.jsp"); 
  27. System.out.println(co); 
  28. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
  29. doPost(request,response); 

 

責(zé)任編輯:武曉燕 來源: 見賢思編程
相關(guān)推薦

2009-06-25 14:54:22

Servlet轉(zhuǎn)發(fā)Servlet重定向

2020-12-09 11:10:12

shellLinux管道

2010-12-14 15:07:15

ICMP路由重定向

2022-11-10 15:08:44

Linux輸入輸出

2009-06-30 15:37:27

Servlet和JSP

2023-02-09 07:01:35

轉(zhuǎn)發(fā)重定向Java

2017-08-08 09:17:41

301302重定向

2024-07-26 08:23:02

2009-11-23 18:39:17

PHP重定向

2022-09-02 08:03:44

IO程序網(wǎng)卡

2010-07-13 14:10:44

ICMP協(xié)議

2016-08-23 17:21:51

UnixLinux重定向

2010-03-09 16:11:59

Linux重定向

2017-01-19 19:14:20

Linux重定向命令

2022-07-18 07:11:35

請求轉(zhuǎn)發(fā)請求重定數(shù)據(jù)共享

2020-07-27 07:41:23

Linux重定向數(shù)據(jù)流

2010-12-31 13:35:25

文件夾重定向

2011-06-15 14:43:43

301重定向

2017-12-06 10:15:27

跳轉(zhuǎn)機(jī)制Chrome

2012-12-24 09:46:50

RDS打印重定向
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號