使用状态码302和location头完成重定向
代码:
@WebServlet("/demo3")
public class ServletDemo3 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//向班长借钱
System.out.println("去找富班长");
//返回没钱
response.setStatus(302);
//找富班长 重定向地址:客户端的绝对路径(包含项目名称)
response.setHeader("location", "/Servlet/demo4");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
@WebServlet("/demo4")
public class ServletDemo4 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//找到富班长
System.out.println("富班长:借钱");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
特点:访问的是demo3,但是最终地址栏变成了demo4。所以这是一次重定向是两次请求和两次响应。
需求:判断登录的html页面的用户名和密码是否都是admin,如果是,登录成功,如果不是,登陆失败(重定向到登录页面)
/**
* 处理登陆功能(重定向)
* @author 58351
*
*/
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//先从客户端接收用户床过来的数据
String username = request.getParameter("username");
String password = request.getParameter("password");
//判断用户名和密码是否都是admin
//如果都是admin,登录成功 否则登录失败,重定向到登录页面
if("admin".equals(username)&&"admin".equals(password)){
//登录成功
response.setStatus(302);
response.setHeader("location", "/Servlet/html/success.html");
}
else{
//登录失败
//重定向
response.setStatus(302);
response.setHeader("location", "/Servlet/html/login.html");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
response对象提供了一个方法直接完成重定向功能:sendRedirect(String location);
使用转发从一个Servlet转发到另一个Servlet
RequestServlet1
package com.request;
import java.io.IOException;
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;
/**
* Servlet implementation class RequsetDemo1
*/
@WebServlet("/request1")
public class RequsetDemo1 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//访问request1,转发到request2
//获取RequestDispatcher对象
RequestDispatcher dispatcher = request.getRequestDispatcher("/request2");
//使用forward方法转发
dispatcher.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
RequestDemo2
package com.request;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class RequestDemo2
*/
@WebServlet("/request2")
public class RequestDemo2 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//向页面输出内容
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("转发成功");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
1、重定向时地址栏会发生变化,转发的地址栏不会发生变化
2、重定向是两次请求和两次响应,转发是一次请求和一次响应
3、重定向不能使用request域传递值,转发可以,因为一次请求会创建一个新的request对象,而重定向中有两次请求,会生成新的request对象
4、重定向的路径中有项目名(/Servlet/request2),转发的路径中没有项目名(/request)
5、重定向可以定向到任何页面(网络上的资源),转发只能在Web应用内部