jsp中<jsp:forward page=""/>和response.sendRedirect("")两种跳转的区别

本文详细解析了JSP中使用<jsp:forward page=/>和response.sendRedirect()两种跳转方式的原理、特点和应用场景区别,包括地址栏变化、跳转速度、请求对象共享情况、参数传递方式等关键信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

jsp中<jsp:forward page=""/>和response.sendRedirect("")两种跳转的区别

一、response.sendRedirect("")

         始终要记住一点就是这种跳转是让客户端浏览器自己去访问指定的页面,这个指定页面的地址就是sendRedirect("")参数的内容。如果跳转的页面是webapp里面的页面,客户端需要两层访问服务器。如果是其他网址的话,客户端则不再访问此服务器,转而访问其他的服务器。是服务器端与客户端的两次往返。速度还是慢些。

(1)地址栏内容有变化。

(2)sendRedirect("")后的语句依旧执行,所以在此语句后面的有向客户端传送信息的语句时候会出现闪烁现象,一般情况下可以做个判断来,如果怎么样则转向指定的页面,否则执行其他的代码。

(3)sendRedirect("")里的参数可以是网页也可以是jsp页面等。

           如sendRedirect(“http://www.baidu.com”)//转到webapp外面去。

               sendRedirect(“a.jsp”)     //转到webapp里面的其他页面去。

(4)如果写成sendRedirect(“/a.jsp”)此时的/代表的是http://127.0.0.1/,它并不包含这个页面所在的webapp。所以要在/后面加上a.jsp的路径。

(5)它们不共用一个request对象。而且跳转页面a.jsp是接收不到传递给包含此跳转指令的页面的参数的。但是包含此跳转指令的页面可以向跳转页面中传递参数,此时跳转页面a.jsp能够接收到传递的参数。如sendRedirect(“a.jsp?name=123”),所以说用此种跳转的话可以让包含此跳转指令的页面接收到参数,然后将参数再往跳转页面中传递。

二、<jsp:forward  page= ""/>

     始终要记住一点就是这种跳转是让服务器端内部进行跳转。而且只能在服务器的内部转换,并将跳转到最后一个页面的执行结果返回给客户端,速度快。

(1) 地址栏内容没有变化。

(2) <jsp:forward page=""/>后面的语句不会被执行的。

(3)如果写成<jsp:forward page=“/a.jsp”/>,此时的/代表的是http://127.0.0.1/工程名此时在后面要跟上a.jsp的路径。

(4)跳转页面a.jsp和包含跳转页面不共用一个request对象。但是跳转页面可以访问到传递给包含此跳转指令的页面的参数的, 跳转的页面的request包含的参数比包含跳转指令的页面的参数要大。这种情况和静态包含和动态包含相似。

(5)包含此跳转指令的页面可以向要跳转到的页面传递参数。

     传参方式一:<jsp:forward page=“a.jsp?param=123”/>时被包含的jsp页面是可以访问该参数的。

     传参方式二:

                          <jsp:forward page=“a.jsp”>

                                     <jsp:param name=“” value=“”/>

                         </ jsp:include >

jsp <%@ page contentType="text/html; charset=utf-8" language="java"%> <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>登录</title> </head> <body> <h2>用户登录</h2> <form action="login_do.jsp" method="post"> 用户名:<input type="text" name="username"/><br/> 密码:<input type="password" name="password"/><br/> <input type="submit" value="登录"/> </form> </body> </html> login_do.jsp (关键修复): jsp <%@ page contentType="text/html; charset=utf-8" language="java"%> <%@ page import="javax.servlet.http.*" %> <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>登录结果</title> </head> <body> <% String username = request.getParameter("username"); String password = request.getParameter("password"); if(username != null) username = username.trim(); if(password != null) password = password.trim(); if("x".equals(username) && "123".equals(password)) { // 创建会话并存储属性 HttpSession session = request.getSession(); session.setAttribute("username", username); // 修复:添加正确的重定向路径 response.sendRedirect(request.getContextPath() + "/welcome.jsp"); } else { out.println("用户名或密码不正确,5秒后为您跳转回登录页面..."); out.println("<meta http-equiv='refresh' content='5;url=" + request.getContextPath() + "/login.jsp'>"); } %> </body> </html> welcome.jsp (关键修复): jsp <%@ page contentType="text/html; charset=utf-8" language="java"%> <%@ page import="javax.servlet.http.*" %> <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>欢迎</title> </head> <body> <% HttpSession session = request.getSession(false); String user = (session != null) ? (String) session.getAttribute("username") : null; if(user == null) { out.println("您必须先登录,5秒后为您跳转回登录页面..."); out.println("<meta http-equiv='refresh' content='5;url=" + request.getContextPath() + "/login.jsp'>"); } else { // 仅在用户已登录时显示欢迎信息 %> <h1>欢迎您,<%= user %>!</h1> <% } %> </body> </html>这三段是eclipse中web的代码,为什么运行之后会显示HTTP ERROR 404 Not Found URI: /ss/login_do.jsp STATUS: 404 MESSAGE: Not Found SERVLET: - Powered by Jetty:// 12.0.15
最新发布
06-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值