文章正文

servlet、jsp、JavaScript跳转的几种方式

【文档】2020-04-23

简介servlet、jsp、JavaScript跳转的几种方式

1. RequestDispatcher.forward(),重定向后浏览器地址栏URL不变。

public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
response.setContentType("text/html; charset=gb2312");
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher("/index.jsp"); //定向的页面
rd.forward(request, response);
}

2. response.sendRedirect(),重定向后在浏览器地址栏上会出现重定向页面的URL。

public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
response.setContentType("text/html; charset=gb2312");
response.sendRedirect("/index.jsp");
}

3. 修改HTTP header的Location属性来重定向

<%
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
String newLocn = "/test/indes.jsp";
response.setHeader("Location",newLocn);
%>

4. JSP中实现在某页面停留若干秒后,自动重定向到另一页面

<meta http-equiv="refresh" content="300; url=target.jsp">
释义:在300秒之后正在浏览的页面将会跳转到target.html。


5、Js 页面跳转(父页面,外层页面,本页面)

"window.location.href"、"location.href"是本页面跳转
"parent.location.href"是上一层页面跳转
"top.location.href"是最外层的页面跳转


6、Js 控制页面跳转的几种方法

//
<script language="javascript" type="text/javascript">
     window.location.href="login.jsp?backurl="+window.location.href; 
</script>

//
<script language="javascript">
 alert("返回");
 window.history.back(-1);
</script>

//
<script language="javascript">
window.navigate("top.jsp");
</script>

//
<script language="JavaScript">
          self.location='top.htm';
</script>

//
<script language="javascript">
          alert("非法访问!");
          top.location='xx.jsp';
</script>

//
<script type="text/javascript">
if (self != top) {
	top.location = self.location;
}; 
</script>

打赏支持

感谢您的支持,加油!

打开微信扫码打赏,你说多少就多少

找书费时,联系客服快速查找!

扫码支持

在线客服8:30-22:30,若离线请留言!

获取教程,请联系在线客服!

扫码支持

在线客服8:30-22:30,若离线请留言!

热门阅读

找PDF电子书,太费时间?

  • 微信扫描二维码,让客服快速查找。
  • 在线客服8:30-22:00,若离线请留言!