重定向 一个web 资源收到客户端请求后,他会通知客户端去访问另外一个web资源 这个过程就叫做重定向。
常见场景:
这是测试的效果图
先是测试类:
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class RedirectTest extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("进入啦这个请求");
// 这就是一个打印 拿来测试的
String username = req.getParameter("username");
//这是获取输入流的名字
String password = req.getParameter("password");
//这是获取输入流的密码
System.out.println(username+":"+password);
resp.sendRedirect("/hui/RedirectTest.jsp");
//这是重定向
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
接下来是自己的写的登录的jsp页面
<%--这是写的那个登录页面--%>
<html>
<body>
<h2>Hello World!</h2>
<%--这里提交的路径, 需要找到项目的路径--%>
<%--${pageContext.request.contextPath}当前项目路径--%>
<form action="${pageContext.request.contextPath}/login" method="get">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit">
</form>
</body>
</html>
这里就是那个跳转后的的页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>RedirectTest</h1>
</body>
</html>