- 超链接 - 用于客户端跳转,最简单。
response.sendRedirect()- 用于服务器端重定向,也是最常见的跳转方式。RequestDispatcher.forward()- 用于服务器端转发,共享同一个请求。
环境准备
为了让你能直接运行示例,我们先创建一个标准的 Java Web 项目结构,假设你使用的是像 Eclipse (for J2EE) 或 IntelliJ IDEA 这样的 IDE。

项目结构:
JspPageRedirectDemo/
├── src/
│ └── com/
│ └── example/
│ └── model/
│ └── User.java
├── WebContent/
│ ├── index.jsp
│ ├── welcome.jsp
│ ├── profile.jsp
│ ├── login.jsp
│ ├── dashboard.jsp
│ ├── css/
│ │ └── style.css
│ └── WEB-INF/
│ └── web.xml
└── pom.xml (如果你使用 Maven)
超链接 - 客户端跳转
这是最简单直接的方式,本质上是 HTML 的 <a> 标签,当用户点击链接时,浏览器会直接向服务器请求新的页面。
特点:
- 类型:客户端跳转。
- URL变化:浏览器的地址栏会变成目标页面的 URL。
- 请求:发起了两次独立的 HTTP 请求。
- 数据共享:无法通过
request对象共享数据(除非使用 URL 参数或 Session)。
示例代码:

index.jsp (首页)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>首页 - 使用超链接跳转</title>
</head>
<body>
<h2>方式一:超链接跳转</h2>
<p>点击下面的链接,浏览器会直接跳转到新页面。</p>
<!-- 这是一个普通的HTML超链接 -->
<a href="welcome.jsp">跳转到欢迎页</a>
<hr>
<!-- 也可以带参数 -->
<a href="welcome.jsp?username=张三">带参数跳转到欢迎页</a>
</body>
</html>
welcome.jsp (目标页面)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>欢迎页</title>
</head>
<body>
<h1>欢迎来到欢迎页面!</h1>
<%
// 获取URL中传递的参数
String username = request.getParameter("username");
if (username != null && !username.isEmpty()) {
out.println("<p>你好, " + username + "!</p>");
}
%>
<a href="index.jsp">返回首页</a>
</body>
</html>
response.sendRedirect() - 服务器端重定向
这是在 Java 代码中最常用的跳转方式,它告诉浏览器:“你请求的页面已经不存在了,请向这个新的 URL 重新发起请求”。
特点:

- 类型:服务器端重定向。
- URL变化:浏览器的地址栏会变成目标页面的 URL。
- 请求:发起了两次独立的 HTTP 请求。
- 数据共享:无法通过
request对象共享数据(因为request对象是新的),数据可以通过 URL 参数或 Session 传递。
示例代码:
login.jsp (登录页面)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>登录页面</title>
</head>
<body>
<h2>方式二:response.sendRedirect() 重定向</h2>
<form action="LoginServlet" method="post">
用户名: <input type="text" name="username"><br>
密码: <input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
LoginServlet.java (处理登录的 Servlet)
package com.example.controller;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1. 获取请求参数
String username = request.getParameter("username");
String password = request.getParameter("password");
// 2. 简单的模拟验证逻辑
if ("admin".equals(username) && "password".equals(password)) {
// 登录成功,重定向到仪表盘页面
// 使用 ?username=... 来传递数据
response.sendRedirect("dashboard.jsp?username=" + username);
} else {
// 登录失败,重定向回登录页面,并附带一个错误信息
response.sendRedirect("login.jsp?error=1");
}
}
}
dashboard.jsp (登录成功后的页面)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>用户仪表盘</title>
</head>
<body>
<h1>用户仪表盘</h1>
<%
String error = request.getParameter("error");
if ("1".equals(error)) {
out.println("<p style='color:red;'>用户名或密码错误!</p>");
}
String username = request.getParameter("username");
if (username != null) {
out.println("<p>欢迎回来, " + username + "!</p>");
}
%>
<a href="login.jsp">重新登录</a>
</body>
</html>
RequestDispatcher.forward() - 服务器端转发
这种方式是在服务器内部将请求从一个资源“传递”给另一个资源,而客户端(浏览器)对此毫不知情。
特点:
- 类型:服务器端转发。
- URL变化:浏览器的地址栏不会改变,仍然是原始请求的 URL。
- 请求:只发起了一次 HTTP 请求。
- 数据共享:可以完美地通过
request对象共享数据(使用request.setAttribute()和request.getAttribute())。
示例代码:
profile.jsp (用户资料页面,但需要先转发处理)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.example.model.User" %>
<%-- 注意:这里的代码可能由转发而来,不一定直接由用户访问 --%>
<html>
<head>用户资料</title>
</head>
<body>
<h2>方式三:RequestDispatcher.forward() 转发</h2>
<%
// 从 request 中获取由 Servlet 设置的属性
User user = (User) request.getAttribute("user");
if (user != null) {
out.println("<p>用户ID: " + user.getId() + "</p>");
out.println("<p>用户名: " + user.getUsername() + "</p>");
out.println("<p>邮箱: " + user.getEmail() + "</p>");
} else {
out.println("<p>没有找到用户信息。</p>");
}
%>
<a href="index.jsp">返回首页</a>
</body>
</html>
ProfileServlet.java (处理用户资料的 Servlet)
package com.example.controller;
import com.example.model.User;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/ProfileServlet")
public class ProfileServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1. 模拟从数据库获取用户数据
User user = new User(101, "李四", "lisi@example.com");
// 2. 将数据存入 request 对象,作为属性
request.setAttribute("user", user);
// 3. 获取转发器,目标为 profile.jsp
RequestDispatcher dispatcher = request.getRequestDispatcher("profile.jsp");
// 4. 执行转发
dispatcher.forward(request, response);
}
}
User.java (一个简单的 JavaBean 模型)
package com.example.model;
public class User {
private int id;
private String username;
private String email;
public User(int id, String username, String email) {
this.id = id;
this.username = username;
this.email = email;
}
// Getters
public int getId() 