转发和重定向详解
转发与重定向的区别
前言:之前写了几篇JSP的博客,现在又回过头来看Servlet,温故而知新,再回顾回顾,总会有收获的。以前学习Servlet感觉内容很多,现在看的时候,其实也没多少东西,只需知道Servlet的生命周期,Servlet的实现方式,ServletContext作用域,接收和响应,转发和重定向,我觉得差不多够用了。当然,要是细究的话也不止这些,我针对的是新手。
转发与重定向简介
转发和重定向都是实现页面跳转,也就是说,当我们访问一个Servlet 的时候,Servlet帮我们跳转到另一个界面。
转发与重定向的区别
实现转发调用的是HttpServletRequest对象中的方法
实现重定向调用的是HttpServletResponse对象中的方法
转发时浏览器中的url地址不会发生改变
重定向时浏览器中的url地址会发生改变
转发时浏览器只请求一次服务器
重定向时浏览器请求两次服务器
转发能使用request带数据到跳转的页面
重定向能使用ServletContext带数据到跳转的页面
代码演示转发和重定向
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | 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 { //获取表单提交过来的数据 //getParameter()方法可以获取请求的参数信息 String name = req.getParameter( "name" ); String password = req.getParameter( "password" ); //打印获取到的参数信息 System.out.println( "name:" +name); System.out.println( "password:" +password); //如果name=admin,password=123,则跳转到succee.jsp,否则跳转到fail.jsp if ( "admin" .equals(name)&& "123" .equals(password)){ //通过转发实现跳转 req.getRequestDispatcher( "/success.jsp" ).forward(req,resp); } else { //通过重定向实现跳转 resp.sendRedirect( "/fail.jsp" ); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } } |
JSP代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | <%@ page contentType= "text/html;charset=UTF-8" language= "java" %> <html> <head> <title>登录</title> </head> <body> <form action= "/login" > <table align= "center" > <tr> <td>账号:</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> |
转发和重定向如何带数据到某个页面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | 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 { //通过转发带数据 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <%@ page contentType= "text/html;charset=UTF-8" language= "java" %> <html> <head> <title>转发和重定向传代数据练习</title> </head> <body> <% //1、接收转发传代的数据 String name = (String) request.getAttribute( "name" ); out.println( "转发传代的数据:" +name); %> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | 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 { //通过重定向带数据 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <%@ page contentType= "text/html;charset=UTF-8" language= "java" %> <html> <head> <title>转发和重定向传代数据练习</title> </head> <body> <% //1、接收重定向传代的数据 String name1 = (String)application.getAttribute( "name" ); out.println( "重定向传代的数据:" +name1); %> </body> </html> |
练习
index.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <%@ page contentType= "text/html;charset=UTF-8" language= "java" %> <html> <head> <title>Title</title> </head> <body> <form action= "CountServlet" method= "post" > <h3>加法计算器</h3> 加数 1 :<input type= "number" name= "one" > 加数 2 :<input type= "number" name= "two" > <input type= "submit" value= "计算" > </form> </body> </html> |
count.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <%@ page contentType= "text/html;charset=UTF-8" language= "java" %> <html> <head> <title>Title</title> </head> <body> 计算结果:<%=request.getAttribute( "count" )%> <!--计算结果:<%=application.getAttribute( "count" )%>--> </body> </html> |
Servlet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | 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); //强制转换,将String类型的数据转换成int类型 String two=request.getParameter( "two" ); int t=Integer.parseInt(two); //强制转换,将String类型的数据转换成int类型 System.out.println(one+ " " +two); int c=o+t; String co=String.valueOf(c); //将int类型的数据转换成String类型 //转发,可以携带数据 request.setAttribute( "count" ,co); request.getRequestDispatcher( "count.jsp" ).forward(request,response); //用于存放数据 // ServletContext s=this.getServletContext(); // s.setAttribute("count",co); //重定向只能依靠ServletContext获取数据 // response.sendRedirect("count.jsp"); System.out.println(co); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } } |
结束语
Servlet分享到此结束,想了解更多的朋友可以看之前总结过的博客《Servlet》,和《JSP》 ,更多精彩等你来学习。
*****************************************************************************************************
我的博客园地址:https://www.cnblogs.com/zyx110/
本文已独家授权给脚本之家(jb51net)公众号独家发布

作者:泰斗贤若如
微信公众号:去有风的地方飞翔
Github:https://github.com/zyx110
有事微信:zyxt1637039050
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。

我不能保证我所说的都是对的,但我能保证每一篇都是用心去写的,我始终认同: “分享的越多,你的价值增值越大”,我们一同在分享中进步,在分享中成长,越努力越幸运。再分享一句话“十年前你是谁,一年前你是谁,甚至昨天你是谁,都不重要。重要的是,今天你是谁,以及明天你将成为谁。”
人生赢在转折处,改变从现在开始!
支持我的朋友们记得点波推荐哦,您的肯定就是我前进的动力。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?