一、使用href超链接标记(客户端跳转)
二、提交表单(客户端跳转)
<!--使用input标签中的submit属性--> <form name="form" method="post" action="page2.jsp"> <input type="submit" value="跳转1"> </form> <!--使用input标签中的button属性--> <form id="formid" name="form" method="post" action="page2.jsp"> <input type="text" id="userid" name="userid" placeholder="请输入用户名"> <input type="password" id="passid" name="passid" placeholder="请输入密码"> <input type="button" value="跳转1 onclick = "checkUser();"> </form>
<script type="text/javascript">
function checkUser(){
var result = document.getElementById(“userid”).value;
var password = document.getElementById(“passid”).value;
if(result == “” ){
alert(“用户名不能为空”);
return false;
}
if(password == “” ){
alert(“密码不能为空”);
return false;
}
document.getElementById(“formid”).submit();
}
</script>三、Javascrip事件(客户端跳转)
<input type="button" value="跳转2" onclick="next()">
<script type="text/javascript">
function next(){
window.location = "page2.jsp";
}
</script>四、使用response对象(客户端跳转)(重定向)
<% response.sendRedirect("page2.jsp"); %> //sendRedirect()可以带参数传递,后面应该紧跟一句return
<% response.setHeader("Refresh", "1;url=page2.jsp"); %> //1秒后,刷新,并跳到,page2.jsp页面五、使用forward动作标记(服务器端跳转)(转发)
<jsp:forward page="page2.jsp" /> //jsp自带的forword标签来实现跳转
六、使用RequestDispatcher类(服务器端跳转)(转发)
<% request.getRequestDispatcher("page2.jsp").forward(request, response);%> response重定向和forward跳转和RequestDispatcher的区别
(1) response重定向
执行完页面的所有代码,再跳转到目标页面。
跳转到目标页面后,浏览器地址栏中的URL会改变。
在浏览器端重定向。
可以跳转到其它服务器上的页面,response.sendRedirect(“http://www.baidu.com”)
(2) forward跳转
forward动作标记之后的代码,不再执行,直接跳转到目标页面。
跳转到目标页面后,浏览器地址栏中的URL不会改变。
在服务器端重定向。
无法跳转到其它服务器上的页面。
指定目标页面时,既可以使用绝对路径,也可以使用相对路径。
(3) RequestDispatcher跳转
执行完所有代码,包括RequestDispatcher之后的所有代码,再跳转到目标页面。
跳转到目标页面后,浏览器地址栏中的URL不会改变。
在服务器端重定向。
无法跳转到其它服务器上的页面。
指定目标页面时,只能使用绝对路径。


