重定向(Redirect)和请求转发(Forward)都是在Web开发中用来处理页面跳转的方式。
从过程图可以看出,重定向会发出两次请求,产生两次响应。
response.sendRedirect("/index.jsp");
在示例中,实现用户注册功能,注册成功则跳转到登录页面,注册失败则跳转到注册页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录</title>
</head>
<body>
<jsp:include page="header.jsp"></jsp:include>
<table cellspacing="10" align="center" background="picture.jpg" width="600px" height="150px">
<form action="/LoginServlet" method="post">
<caption align="center" font-size="30px">用户登录</caption>
<tr>
<td>用户名:</td>
<td><input type="text" name="username" value="${user.username}"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password" value="${user.password}"></td>
</tr>
<tr>
<td align="center"><input type="submit" value="登录"></td>
<td align="center"><input type="button" value="注册" onclick="location.href='./register.jsp' "></td>
</tr>
</form>
</table>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册</title>
</head>
<body>
<jsp:include page="header.jsp"></jsp:include>
<h2 align="center">用户注册</h2>
<table cellspacing="10" align="center" background="picture.jpg" width="600px" height="150px">
<form action="/RegisterServlet" method="post">
<tr>
<td>用户名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td align="center"><input type="submit" value="注册"></td>
<td align="center"><input type="button" value="登录" onclick="location.href='/index.jsp' "></td>
</tr>
</form>
</table>
</body>
</html>
package com.cn.servlet;
import com.cn.bean.User;
import com.cn.service.UserService;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet(name = "RegisterServlet", value = "/RegisterServlet")
public class RegisterServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取从浏览器中传递过来的参数
String username = request.getParameter("username");
String password = request.getParameter("password");
User user = new User();
user.setUsername(username);
user.setPassword(password);
UserService userService = new UserService();
if(userService.registerUser(user)){
HttpSession session = request.getSession();
session.setAttribute("msg","注册成功,请登录");
response.sendRedirect("/index.jsp");
}else {
HttpSession session = request.getSession();
session.setAttribute("msg","用户名重复,请重新注册");
response.sendRedirect("/register.jsp");
}
}
}
它会提示你用户名重复,并且重新跳转到注册界面。
它会提示你注册成功,请登录,并且跳转到登录界面。我们可以发现,注册成功之后网址也发生了改变。
从过程图可以看出,请求转发会发出一次请求,产生一次响应。
request.getRequestDispatcher("/ShowStudentServlet").forward(request,response);
在示例中,我们添加学生信息,添加成功之后还要把所有学生信息展示出来。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加学生</title>
</head>
<body>
<table align="center" width="600" height="400">
<form action="/AddStudentServlet" method="post">
<tr>
<td>姓名:</td>
<td>
<input type="text" name="name">
</td>
</tr>
<tr>
<td>年龄:</td>
<td>
<input type="text" name="age">
</td>
</tr>
<tr>
<td>性别:</td>
<td>
<input type="radio" name="gender" value="男" />男
<input type="radio" name="gender" value="女" />女
</td>
</tr>
<tr>
<td>爱好:</td>
<td>
<input type="text" name="hobby">
</td>
</tr>
<tr>
<td>城市:</td>
<td>
<input type="text" name="city">
</td>
</tr>
<tr>
<td>
<input type="submit" value="提交">
</td>
<td>
<input type="button" value="重置" onclick="location.href='/addStudent.jsp' ">
</td>
</tr>
</form>
</table>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>success</title>
</head>
<body>
欢迎:${user.username}
<table align="center">
<form action="/SearchStudentServlet" method="post">
<tr>
<td>
<input type="text" name="content" placeholder="请输入学号或姓名" style="align-content: center">
</td>
<td>
<input type="submit" value="搜索">
</td>
</tr>
</form>
</table>
<table align="center">
<tr>
<td>
<input type="button" value="添加" onclick="location.href='addStudent.jsp' ">
</td>
<form action="/ShowStudentServlet" method="post">
<td>
<input type="submit" value="刷新">
</td>
</form>
<td>
<input type="button" value="退出" onclick="location.href='/LogoutServlet' " >
</td>
</tr>
</table>
<table border="1" width="500px" height="400px" align="center">
<tr>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>爱好</th>
<th>城市</th>
<th>操作</th>
<th>操作</th>
</tr>
<c:forEach var="student" items="${studentList}">
<tr>
<td align="center">
${student.id}
</td>
<td align="=center">
${student.name}
</td>
<td align="center">
${student.age}
</td>
<td align="center">
${student.gender}
</td>
<td align="center">
${student.hobby}
</td>
<td align="center">
${student.city}
</td>
<td align="center">
<button onclick="location.href='/QueryStudentServlet?id=${student.id}'">修改</button>
</td>
<td align="center">
<button onclick="location.href='/DeleteStudentServlet?id=${student.id}'">删除</button>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
package com.cn.servlet;
import com.cn.bean.Student;
import com.cn.service.StudentService;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet(name = "AddStudentServlet", value = "/AddStudentServlet")
public class AddStudentServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取从form表单传递过来的参数
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
String gender = request.getParameter("gender");
String hobby = request.getParameter("hobby");
String city = request.getParameter("city");
Student student = new Student();
student.setName(name);
student.setAge(age);
student.setGender(gender);
student.setHobby(hobby);
student.setCity(city);
StudentService studentService = new StudentService();
studentService.insertStudent(student);
request.getRequestDispatcher("/ShowStudentServlet").forward(request,response);
}
}
package com.cn.servlet;
import com.cn.bean.Student;
import com.cn.service.StudentService;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
@WebServlet(name = "ShowStudentServlet", value = "/ShowStudentServlet")
public class ShowStudentServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
StudentService studentService = new StudentService();
//获取session中传递过来的数据
HttpSession session = request.getSession();
session.getAttribute("user");
ArrayList<Student> studentList = studentService.findStudent();
request.setAttribute("studentList",studentList);
request.getRequestDispatcher("/welcome.jsp").forward(request,response);
}
}
我们添加一个学生,结果如下:
我们可以发现,添加成功后url地址不会发送变化。