JSP 页面跳转的实现方法

客户端跳转

1. 使用 href 超链接标记  <a href="new.jsp">跳转</a>

2. 使用表单提交完成跳转  <form action="new.jsp"> ... </form>

3. 使用 JavaScript 

4. 使用 response.sendRedirect(String path) 重定向

服务器端跳转

5. 使用 request.getRequestDispatcher("目标页面").forward(request, response) 转发

6. <jsp:forward page="目标页面"> 该方法底层实现是通过request.getRequestDispatcher("目标页面").forward(request, response)

--------------------------------------------------------------------------------------------------------------------------------------------

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

这个比较简单,通常写到 <a> 标签中即可。

<a> 标签定义超链接,用于从一个页面链接到另一个页面

<a> 元素的最重要的属性是 href 属性,用于指定链接的目标

适用于完成指定位置的动态跳转

<a href="new.jsp">跳转</a>
<a href="http://www.runoob.com">访问菜鸟教程!</a>

2. 通过表单提交完成跳转(客户端跳转)

将整个 form 表单数据提交到方式

<form name="form1" method="POST" action="new.jsp">
    <input type="text" name="name" />
    <input type="text" name="pad" />
    <input type="submit" value="提交" />
    <input type="reset" value="重置">
</form>

3. 使用 JavaScript(客户端跳转)

1)可以在 JavaScript 代码里面写提交表单的写法

<script type="text/javascript">
    function submit() {
        with(document.getElementById("queryFunction")){
            action="new.jsp";
            method="post";
            submit();
        }
    }
</script>

2)可以直接定位,给 window.location 赋值,而不是提交表单

使用 replace() 方法与定位  window.location 的差别是前者没有历史记录

<script type="text/javascript">
    function go{
        window.self.location="new.jsp";
    }
</script>
<!-- 或者 -->
<script type="text/javascript">
    window.location.replace("http://www.baidu.com");
</script>

3)使用 history 对象的 forword(),back(),go() 方法

其中 go() 方法需要一个整型入口参数

下面两句代码的功能相同

<a href="javascript:history.back()">返回上一步</a>
<a href="javascript:history.go(-1)">返回上一步</a>

4. 使用 JSP 的内置对象 response(客户端跳转)

1)直接使用 sendRedirct() 重定向,重定向后在浏览器地址栏上会出现重定向页面的 URL

sendRedicrect() 中的 URL 可以是带参数的,例如 response.sendRedirect("url?name=" + name),因此我们可以在跳转到时候传递参数。

一般 response.Redirect() 之后会紧跟一句 return; 我们已经知道 response.sendRedirect() 是通过浏览器来完成跳转的,所以在页面处理完成后才会有实际的动作。

既然已经跳转了,那么后面的输出就已经没有意义了,而且有可能会因为后面的输出导致跳转的失败。

<%
    response.sendRedirect("http://www.baidu.com");
    return;
%>

2)使用 setHeader() 方法,直接修改地址栏,同样可以实现页面的重定向

标准格式:response.setHeader("Refresh", "等待的秒数; url=绝对路劲或者相对路径");

<%
    response.setHeader("refresh", "1;url=http://www.baidu.com");
%>

5. 使用 requestDispatcher 类(服务器端跳转)

Servlet 可以通过两种方式得到 RequestDispatcher 对象:

-- ServletContext 的 getRequestDispatcher(String path)   path为绝对路径

-- ServletRequest 的 getRequestDispatcher(String path)    path可以为绝对路径,也可以为相对路径

RequestDispatcher rd = request.getRequestDispatcher("目标页面");
rd.forward(request, response);

response.sendRedirect(String path) 重定向和 requestDispatcher.forward(request, response) 跳转的区别

response.sendRedirect(path) 重定向:

1)在浏览器中重定向

2)执行完所有的代码后再跳转到目标页面

3)跳转到目标页面后浏览器地址栏的URL会改变

4)可以跳转到其他服务器上的页面,如 “http://www.baidu.com”

5)通常和 return 配合使用

requestDispatcher.forward(request, response)

1)在服务器端转发

2)直接跳转到目标页面,其后的代码不再执行

3)跳转到目标页面后浏览器地址栏的URL不会改变

4)无法跳转到其他服务器的页面

5)当身处循环或者可能有多个 forword() 需要和 return 配合使用

原文地址:https://www.cnblogs.com/0820LL/p/10595000.html