转发的原理及代码演示
2024-01-03 14:39:29
一、转发的概念
一个Web组件(Servlet/JSP)将未完成的处理通过容器转交给另外一个Web组件继续处理,转发 的各个组件会共享Request和Response对象。
二、代码演示
1、编写转发界面
-
创建空工程,在工程中创建javaEE模块
-
配置中设置tomcat的部署
-
编写register界面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>转发测试</title> </head> <body> <form action="forwardServlet" method="post"> <input type="submit" value="转发"> </form> </body> </html>
2、编写Servlet 转发
-
编写servlet
-
ForwardServlet
package com.example.forward_demo03; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.IOException; @WebServlet(name = "ForwardServlet", urlPatterns = "/forwardServlet") public class ForwardServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("接收到了转发请求!"); //获取另一个web组件,让它进行转发 RequestDispatcher requestDispatcher = request.getRequestDispatcher("/targetServlet"); requestDispatcher.forward(request, response); } }
这里看到,需要再编写一个 targetServlet,处理转发请求。它会执行真正的转发请求。
-
targetServlet
package com.example.forward_demo03; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.IOException; import java.io.PrintWriter; @WebServlet(name = "TargetServlet", value = "/targetServlet") public class TargetServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("接收到了传递过来的转发请求!"); response.setContentType("text/html;charset=utf-8"); PrintWriter writer = response.getWriter(); writer.write("<h1>" + "转发成功!" + "</h1>"); } }
-
-
部署测试:
-
运行tomcat
-
访问转发界面,发送转发请求
-
转发请求发送,浏览器界面显示
-
控制台输出显示
-
F12查看调试信息
可以看到只进行一次请求。
-
3、转发的其他功能
绑定数据到Request对象
-
修改ForwardServlet
package com.example.forward_demo03; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.IOException; @WebServlet(name = "ForwardServlet", urlPatterns = "/forwardServlet") public class ForwardServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("接收到了转发请求!"); request.setAttribute("key1", "value1"); //获取另一个web组件,让它进行转发 RequestDispatcher requestDispatcher = request.getRequestDispatcher("/targetServlet"); requestDispatcher.forward(request, response); } }
-
修改TargetServlet
package com.example.forward_demo03; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.IOException; import java.io.PrintWriter; @WebServlet(name = "TargetServlet", value = "/targetServlet") public class TargetServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("接收到了传递过来的转发请求!"); Object value = request.getAttribute("key1"); System.out.println("获取到的转发来的key1对应的值:" + value); response.setContentType("text/html;charset=utf-8"); PrintWriter writer = response.getWriter(); writer.write("<h1>" + "转发成功!" + "</h1>"); } }
-
测试结果
从这也可以看出,转发的request用的是同一个。
三、转发和重定向的比较
转发 | 重定向 | |
---|---|---|
转发/重定向之后浏览器地址栏的URL会不会发生改变 | 不会 | 会 |
转发/重定向过程中是否共享Request对象 | 是 | 否 |
转发/重定向的URL可不可以是其它项目工程 | 不可以 | 可以 |
文章来源:https://blog.csdn.net/CAUC_lin/article/details/135362261
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!