日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
聊聊Java中的轉(zhuǎn)發(fā)與重定向

 [[390002]]

創(chuàng)新互聯(lián)建站是一家專業(yè)提供滿城企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站設(shè)計、做網(wǎng)站、H5頁面制作、小程序制作等業(yè)務(wù)。10年已為滿城眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡(luò)公司優(yōu)惠進行中。

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

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

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

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

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

  • 實現(xiàn)轉(zhuǎn)發(fā)調(diào)用的是 HttpServletRequest 對象中的方法
  • 實現(xiàn)重定向調(diào)用的是 HttpServletResponse 對象中的方法
    • 轉(zhuǎn)發(fā)時瀏覽器中的 url 地址不會發(fā)生改變
    • 重定向時瀏覽器中的 url 地址會發(fā)生改變
  • 轉(zhuǎn)發(fā)時瀏覽器只請求一次服務(wù)器
  • 重定向時瀏覽器請求兩次服務(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ā)實現(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.  
  3.  
  4. 登錄 
  5.  
  6.  
  7.  
  8.  
  9.  
  10. 賬號: 
  11.  
  12.  
  13.  
  14. 密碼: 
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  

 

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

 
 
 
 
  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.  
  3.  
  4. 轉(zhuǎn)發(fā)和重定向傳代數(shù)據(jù)練習(xí) 
  5.  
  6.  
  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.  
  17.  

 
 
 
 
  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.  
  3.  
  4. 轉(zhuǎn)發(fā)和重定向傳代數(shù)據(jù)練習(xí) 
  5.  
  6.  
  7. <% 
  8. //1、接收重定向傳代的數(shù)據(jù) 
  9. String name1 = (String)application.getAttribute("name"); 
  10. out.println("重定向傳代的數(shù)據(jù):"+name1); 
  11. %> 
  12.  
  13.  

 

 

練習(xí)

 

index.jsp

  
  
  
  
  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %> 
  2.  
  3.  
  4. Title 
  5.  
  6.  
  7.  
  8. 加法計算器

     
  9. 加數(shù)1: 
  10. 加數(shù)2: 
  11.  
  12.  
  13.  
  14.  

count.jsp

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

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);//強制轉(zhuǎn)換,將String類型的數(shù)據(jù)轉(zhuǎn)換成int類型 
  14. String two=request.getParameter("two"); 
  15. int t=Integer.parseInt(two);//強制轉(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); 

當前名稱:聊聊Java中的轉(zhuǎn)發(fā)與重定向
鏈接地址:http://www.dlmjj.cn/article/coghpgp.html