JSP页面之间跳转的几种方式

1 使用href超链接标记 客户端跳转

2 表单提交 客户端跳转

3 使用RequestDispatcher.forward()(转发) 服务器跳转

4 使用 response.sendRedirect()(重定向) 客户端跳转

5 使用forward动作标记 服务器跳转

6 使用response对象实现页面跳转的第二种方法,setHeader()方法

7 使用JavaScript 客户端跳转

第一种:使用href超链接标记

<a href="想要跳转的jsp页面">跳转<a/>
例如:<a href="newPage.jsp">跳转<a/>

<a href="想要跳转的url">跳转<a/>
例如:<a href="https://blog.csdn.net/weixin_43656426">跳转<a/>

第二种:表单提交

<form name="form1" method="POST" action="newPage.jsp">  ------action中可以是jsp页面也可以是url路由
   <input type="text" name="name">  
   <input type="text" name="password">  
   <input type="submit" value="提交">  
</form>

第三种:使用RequestDispatcher.forward()(转发)

request.getRequestDispatcher("newPage.jsp").forward(response,request);
request.getRequestDispatcher("https://blog.csdn.net/weixin_43656426").forward(request,response);

第四种:使用response.sendRedirect()(重定向)

response.sendRedirect("newPage.jsp"); ------jsp页面
response.sendRedirect("https://blog.csdn.net/weixin_43656426");-----url路径

第五种:使用forward动作标记

<jsp:forward page="newPage.jsp" /> 

第六种:使用setHeader()方法

response.setCharacterEncoding("utf-8");  
response.setHeader("iso-8859-1","utf-8");  
request.setCharacterEncoding("utf-8");  
response.setHeader("Refresh","5;url=newPage.jsp");  
response.setHeader("Refresh","等待的秒数;url=绝对路径或者相对路径"); 路径问题sendredirect也一样,绝对路径相对路径都没意见,此句代码是等待5秒之后跳转

第七种:使用JavaScript

Js 页面跳转(父页面,外层页面,本页面)
“window.location.href”、"location.href"是本页面跳转
"parent.location.href"是上一层页面跳转
"top.location.href"是最外层的页面跳转

第一种:

<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>

第七种:

自定义时间跳转(方法一):

<script language="javascript">
var secs = 3; //倒计时的秒数 
var URL ;
function Load(url){
URL = url;
for(var i=secs;i>=0;i--) 
{ 
  window.setTimeout('doUpdate(' + i + ')', (secs-i) * 1000); 
} 
}
function doUpdate(num) 
{ 
document.getElementById('ShowDiv').innerHTML = '将在'+num+'秒后自动跳转到主页' ;
if(num == 0) { window.location = URL; }
}
</script>

然后在里面加上 index.asp为自己要跳转的页面。

在之间加上

自定义时间跳转(方法二):

<p style="text-indent: 2em; margin-top: 30px;">
系统将在 <span id="time">5</span> 秒钟后自动跳转至新网址,如果未能跳转,<a href="http://www.jb51.net" title="点击访问">请点击</a>。 
<script type="text/javascript">  
  delayURL();	
  function delayURL() { 
    var delay = document.getElementById("time").innerHTML;
 var t = setTimeout("delayURL()", 1000);
    if (delay > 0) {
      delay--;
      document.getElementById("time").innerHTML = delay;
    } else {
   clearTimeout(t); 
      window.location.href = "http://www.jb51.net";
    }		
  } 
</script>
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐