方法有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; import javax.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重定向支持跨域访问
原文地址:https://www.cnblogs.com/yunquan/p/10230366.html
时间: 2024-10-13 11:26:52