URL跳转的几种方式

URL跳转的几种方式

1、HTML:

①、

2、javascript:

①、window.location.href方式

②、window.navigate方式跳转

③、window.loction.replace方式

④、self.location方式

⑤、top.location方式

⑥、返回方式

PS:Javascript刷新页面的几种方法: history.go(0) location.reload() location=location location.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(),如果有,页面不会跳转。

跳转后浏览器地址栏变化

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

相关推荐