JSP系列教材 (五)- 服务端跳转和客户端跳转

Servlet的跳转一样,JSP的跳转也分服务端跳转和客户端跳转。

步骤1:首先准备 jump.jsp 
步骤2:客户端跳转
步骤3:服务端跳转

示例 1 : 首先准备 jump.jsp 

首先准备一个jump.jsp 来分别演示客户端跳转和服务端跳转

示例 2 : 客户端跳转

jsp的客户端跳转和Servlet中是一样的。

response.sendRedirect("hello.jsp");


可以通过firefox的调试工具可以观察到访问jump.jsp返回302(临时客户端跳转),跳转到了hello.jsp

客户端跳转

<% 

    response.sendRedirect("hello.jsp");

%>

示例 3 : 服务端跳转

Servlet的服务端跳转一样,也可以使用

request.getRequestDispatcher("hello.jsp").forward(request, response);



或者使用动作,简化代码

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

服务端跳转

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


更多内容,点击了解: https://how2j.cn/k/jsp/jsp-redirect/577.html

原文地址:https://www.cnblogs.com/Lanht/p/12615389.html