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

本篇內(nèi)容介紹了“Java中轉(zhuǎn)發(fā)與重定向的區(qū)別”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

成都創(chuàng)新互聯(lián)公司長(zhǎng)期為上1000+客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開(kāi)放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為義烏企業(yè)提供專業(yè)的成都做網(wǎng)站、成都網(wǎng)站建設(shè),義烏網(wǎng)站改版等技術(shù)服務(wù)。擁有十年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開(kāi)發(fā)。

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

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

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

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

  • 實(shí)現(xiàn)轉(zhuǎn)發(fā)調(diào)用的是 HttpServletRequest 對(duì)象中的方法

  • 實(shí)現(xiàn)重定向調(diào)用的是 HttpServletResponse 對(duì)象中的方法

    • 轉(zhuǎn)發(fā)時(shí)瀏覽器中的 url 地址不會(huì)發(fā)生改變

    • 重定向時(shí)瀏覽器中的 url 地址會(huì)發(fā)生改變

  • 轉(zhuǎn)發(fā)時(shí)瀏覽器只請(qǐng)求一次服務(wù)器

  • 重定向時(shí)瀏覽器請(qǐng)求兩次服務(wù)器

    • 轉(zhuǎn)發(fā)能使用 request 帶數(shù)據(jù)到跳轉(zhuǎn)的頁(yè)面

    • 重定向能使用 ServletContext 帶數(shù)據(jù)到跳轉(zhuǎn)的頁(yè)面

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

package servlet;   import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException;   @WebServlet("/login") public class ServletDemo extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //獲取表單提交過(guò)來(lái)的數(shù)據(jù) //getParameter()方法可以獲取請(qǐng)求的參數(shù)信息 String name = req.getParameter("name"); String password = req.getParameter("password");   //打印獲取到的參數(shù)信息 System.out.println("name:"+name); System.out.println("password:"+password);   //如果name=admin,password=123,則跳轉(zhuǎn)到succee.jsp,否則跳轉(zhuǎn)到fail.jsp if("admin".equals(name)&&"123".equals(password)){ //通過(guò)轉(zhuǎn)發(fā)實(shí)現(xiàn)跳轉(zhuǎn) req.getRequestDispatcher("/success.jsp").forward(req,resp); }else { //通過(guò)重定向?qū)崿F(xiàn)跳轉(zhuǎn) resp.sendRedirect("/fail.jsp"); } }   @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp);   } }

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

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

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

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

JSP代碼

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登錄</title> </head> <body> <form action="/login"> <table align="center"> <tr> <td>賬號(hào):</td> <td><input type="text" name="name"></td> </tr> <tr> <td>密碼:</td> <td><input type="text" name="password"></td> </tr> <tr> <td><input type="submit" value="登錄"></td> <td><input type="reset" value="重置"></td> </tr> </table> </form> </body> </html>

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

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

package servlet; import javax.servlet.ServletContext;  import javax.servlet.ServletException;  import javax.servlet.annotation.WebServlet;  import javax.servlet.http.HttpServlet;  import javax.servlet.http.HttpServletRequest;  import javax.servlet.http.HttpServletResponse;  import java.io.IOException; @WebServlet("/login")  public class ServletDemo extends HttpServlet {  @Override  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //通過(guò)轉(zhuǎn)發(fā)帶數(shù)據(jù)  req.setAttribute("name","張三");  req.getRequestDispatcher("/send.jsp").forward(req,resp); } @Override  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {  doGet(req, resp); }  }

send.jsp

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

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

package servlet; import javax.servlet.ServletContext;  import javax.servlet.ServletException;  import javax.servlet.annotation.WebServlet;  import javax.servlet.http.HttpServlet;  import javax.servlet.http.HttpServletRequest;  import javax.servlet.http.HttpServletResponse;  import java.io.IOException; @WebServlet("/login")  public class ServletDemo extends HttpServlet {  @Override  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {    //通過(guò)重定向帶數(shù)據(jù)  ServletContext servletContext = this.getServletContext();  servletContext.setAttribute("name","王二麻子");  resp.sendRedirect("/send2.jsp"); } @Override  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {  doGet(req, resp); }  }

send2.jsp

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

練習(xí)

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

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="CountServlet" method="post"> <h4>加法計(jì)算器</h4> 加數(shù)1:<input type="number" name="one"> 加數(shù)2:<input type="number" name="two"> <input type="submit" value="計(jì)算"> </form> </body> </html>

count.jsp

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

Servlet

package servlet; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/CountServlet") public class CountServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String one=request.getParameter("one"); int o=Integer.parseInt(one);//強(qiáng)制轉(zhuǎn)換,將String類型的數(shù)據(jù)轉(zhuǎn)換成int類型 String two=request.getParameter("two"); int t=Integer.parseInt(two);//強(qiáng)制轉(zhuǎn)換,將String類型的數(shù)據(jù)轉(zhuǎn)換成int類型 System.out.println(one+" "+two); int c=o+t; String co=String.valueOf(c);//將int類型的數(shù)據(jù)轉(zhuǎn)換成String類型 //轉(zhuǎn)發(fā),可以攜帶數(shù)據(jù) request.setAttribute("count",co); request.getRequestDispatcher("count.jsp").forward(request,response); //用于存放數(shù)據(jù) // ServletContext s=this.getServletContext(); // s.setAttribute("count",co); //重定向只能依靠ServletContext獲取數(shù)據(jù) // response.sendRedirect("count.jsp"); System.out.println(co); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } }

“Java中轉(zhuǎn)發(fā)與重定向的區(qū)別”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

本文名稱:Java中轉(zhuǎn)發(fā)與重定向的區(qū)別
新聞來(lái)源:http://bm7419.com/article8/jcsgop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、標(biāo)簽優(yōu)化企業(yè)網(wǎng)站制作網(wǎng)站制作、Google、定制網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營(yíng)