(二十一):设置页面跳转 (JSP学习第六天)

本文详细介绍了JSP中两种页面跳转方法:客户端跳转(使用response.sendRedirect())和服务端跳转(使用<jsp:forward>)。对比了两者的区别,如地址栏变化、request对象传递等,并给出了实际应用建议。

设置页面跳转

使用response对象中的sendRedirect()方法进行跳转

直接跳转到hello.html页面 response_demo03.jsp

<%@ page language="java" contentType="text/html" pageEncoding="GBK"%>
<html>
<head>
<title>测试</title>
</head>
<body>
<%
 response.sendRedirect("hello.html");
%>
</body>
</html>

这种跳转属于客户端跳转

<jsp:forward>属于服务端跳转,地址不会发生改变,可以将request属性保存到跳转页
response.sendRedirect()属于客户端跳转,地址会发生改变,不可以将request属性保存到跳转页
还有一个区别就是:服务端跳转会立刻跳转,而客户端跳转在整个页面执行完后才进行跳转

服务器端跳转response_demo04.jsp

<%@ page language="java" contentType="text/html" pageEncoding="GBK"%>
<html>
<head>
<title>测试</title>
</head>
<body>
<%
 System.out.println("----------forward跳转之前的-------------");
%>
<jsp:forward page="hello.html"/>
<%
 System.out.println("----------forward跳转之后的-------------");
%>
</body>
</html>

 

显示结果:hello
但tomcat服务器后台显示----------forward跳转之前的-------------

客户端跳转 response_demo05.jsp

<%@ page language="java" contentType="text/html" pageEncoding="GBK"%>
<html>
<head>
<title>测试</title>
</head>
<body>
<%
 System.out.println("----------response跳转之前-------------");
%>
<%
 response.sendRedirect("hello.html");
%>
<%
 System.out.println("----------response跳转之后------------");
%>
</body>
</html>

tomcat服务器后台显示----------response跳转之前-------------
     ----------response跳转之后-------------
     
由于这两种跳转存在差异,所以在开发中,使用JDBC操作中,一定要再<jsp:forward>语句之
前关闭数据库的连接否则再也无法关闭,如果没有关闭,将达到一定程度时,则会出现数据库
已经达到最大的异常,此时就只有重启服务器了

使用<jsp:forward>,可以通过<jsp:param>进行参数传递
而使用response.sendRedirect()方式传递只有通过地址重写的方式传递
所以服务端跳转比客户端跳转更常用!

转载于:https://www.cnblogs.com/techstan/archive/2011/10/06/2199968.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值