java 跳转url_URL跳转的几种方式

本文总结了多种网页跳转的方法,包括HTML、JavaScript、Java(Servlet)及JSP中的实现方式,并详细解释了每种方法的特点及应用场景。

1、HTML:

①、

2、javascript:

①、window.location.href方式

//以下方式直接跳转

window.location.href='hello.html';//以下方式定时跳转

setTimeout("javascript:location.href='hello.html'",5000);

②、window.navigate方式跳转

window.navigate("target.aspx");

③、window.loction.replace方式

window.location.replace("target.aspx");

④、self.location方式

self.location='target.aspx';

⑤、top.location方式

top.location='target.aspx';

⑥、返回方式

alert("返回");

window.history.back(-1);

PS:Javascript刷新页面的几种方法: history.go(0)location.reload()location=locationlocation.assign(location)document.execCommand('Refresh')window.navigate(location)location.replace(location)document.URL=location.href

3、Java类(servlet):

①、response.sendRedirect("/a.jsp");

页面的路径是相对路径。sendRedirect可以将页面跳转到任何页面,不一定局限于本web应用中,如:

response.sendRedirect("http://www.jb51.net");

跳转后浏览器地址栏变化。

这种方式要传值出去的话,只能在url中带parameter或者放在session中,无法使用request.setAttribute来传递。

②、RequestDispatcher dispatcher = request.getRequestDispatcher("/a.jsp");

dispatcher .forward(request, response);

页面的路径是相对路径。forward方式只能跳转到本web应用中的页面上。

跳转后浏览器地址栏不会变化。

跳转到同级目录下的页面。

使用这种方式跳转,传值可以使用三种方法:url中带parameter,session,request.setAttribute

4、JSP:

①、response.sendRedirect();

同上。

②、response.setHeader("Location","");

此语句前不允许有out.flush(),如果有,页面不会跳转。

跳转后浏览器地址栏变化

此语句后面的语句执行完成后才会跳转

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值