5.3 Response 重定向
直接一句代码:resp.sendRedirect("重定向的路径");
resp.sendRedirect("/yzm");
发生错误:
明显是,我们 缺了一部分的 路径字符串。
package com.muquanyu.servlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
public class RedirectServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.sendRedirect("/reponse_war/yzm");
//super.doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//super.doPost(req, resp);
}
}
你会发现 Location 就是我们的 重定向 路径。而 状态码 就是 302。这就是 重定向的一个 头协议。那么 这些我们只去设置 头协议,是否 就可以 实现 相应的 功能呢?答: 是可以的!实际上 sendRedirect() 方法 就是 设置这些 头协议 进行的封装。
5.3.1 面试题:请你聊聊重定向和转发的区别
相同点:
- 页面都会实现跳转
不同点:
- 请求转发的时候,url 不会发生变化。
- 请求转发的时候,url 会发生变化。
主要是因为 转发 是 请求的资源 再去请求 另一个资源,然后最后 反馈 给你的是 最后请求的那个资源。而 重定向 是 请求的资源 提供给你 另一个 资源的 地址,让你去 重新请求 提供的这个 地址的资源!所以 才会 出现 相同点 和 不同点 。
5.3.2 index.jsp 利用表单去提交数据
- 先写一个 接收这个提交数据的 网页
package com.muquanyu.servlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
public class loginTest extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setCharacterEncoding("utf-8");
resp.getWriter().print("提交成功!");
//super.doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//super.doPost(req, resp);
}
}
- 写一下 表单
<html>
<body>
<h2>Hello World!</h2>
<%@page pageEncoding="utf-8"%>
<form action="${pageContext.request.contextPath}/login" method="get">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="psw"><br>
<input type="submit">
</form>
</body>
</html>
${pageContext.request.contextPath}
获取 当前 项目的路径
<%@page pageEncoding="utf-8"%>
设置页面 编码显示
3. 进行 数据的 获取 和 使用数据,并且 再次 尝试 重定向到 jsp 文件。
package com.muquanyu.servlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
public class loginTest extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
//resp.getWriter().print("提交成功!");
String username = req.getParameter("username");
String password = req.getParameter("psw");
System.out.println(username+":"+password);
resp.sendRedirect("/reponse_war/success.jsp");
//super.doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//super.doPost(req, resp);
}
}
- success.jsp
<%--
Created by IntelliJ IDEA.
User: muqua
Date: 2021/8/12
Time: 9:48
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>success</h1>
</body>
</html>