Java Web之Web元件之間的跳轉方式
阿新 • • 發佈:2019-01-06
方法有3種:
第一種:請求轉發
我們來寫兩個類,一個是Servlet1一個是Servlet2
package main.com.vae.forward; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; @WebServlet("/forward/s1") public class Servlet1 extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=utf-8"); PrintWriter out=resp.getWriter(); out.print("1開始"); System.out.println("1開始"); req.getRequestDispatcher("/forward/s2").forward(req,resp); out.print("1結束"); System.out.println("1結束"); } }
package main.com.vae.forward;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; import java.io.PrintWriter; @WebServlet("/forward/s2") public class Servlet2 extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=utf-8"); PrintWriter out=resp.getWriter(); out.print("2開始"); System.out.println("2開始"); out.print("2結束"); System.out.println("2結束"); } }
我們可以看到,兩個Servlet之間使用請求轉發的關鍵程式碼就是
req.getRequestDispatcher("/forward/s2").forward(req,resp);
我們看看瀏覽器訪問的結果
再看看Idea的輸出欄
發現了一個問題了沒?我們Servlet1中寫的響應,就是out物件這個PrintWrite,這個響應沒有在瀏覽器中展示,只有Servlet2的響應存在!
這就是請求轉發的一個特點,最終響應給瀏覽器的由Servlet2決定
其他的特別不多介紹了。
URL重定向
請求轉發是一次請求,URL重定向其實是兩次請求,看圖
把剛才講請求轉發的兩個類複製一下,建立一個redirect包,放進去
package main.com.vae.redirect; 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; import java.io.PrintWriter; @WebServlet("/redirect/s1") public class Servlet1 extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=utf-8"); PrintWriter out=resp.getWriter(); out.print("1開始"); System.out.println("1開始"); //這個是URL重定向,我可以指向/redirect/s2,也可以指向跨域的網址 resp.sendRedirect("/redirect/s2"); //resp.sendRedirect("http://www.baidu.com"); out.print("1結束"); System.out.println("1結束"); } }
package main.com.vae.redirect; 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; import java.io.PrintWriter; @WebServlet("/redirect/s2") public class Servlet2 extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=utf-8"); PrintWriter out=resp.getWriter(); out.print("2開始"); System.out.println("2開始"); out.print("2結束"); System.out.println("2結束"); } }
瀏覽器打/redirect/s1 執行看看結果
直接跳到s2了,URL重定向其實就是把一個URL貼到瀏覽器的框裡敲回車,所以我打百度 也是可以的,所以URL重定向支援跨域訪問