1.首先要对本Java文件的 index.jsp 进行修改
点击查看代码
<html>
<body>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<%-- ${pageContext.request.contextPath}/login 代表当前项目--%>
<%--<%@ page contentType="text/html; charset=UTF-8" %>是为了防止chorme乱码--%>
<%@ page contentType="text/html; charset=UTF-8" %>
<form action="${pageContext.request.contextPath}/login" method="get">
用户名: <input type="text" username="username"> <br>
密码: <input type="text" password="password"><br>
<input type="submit">
</form>
</body>
</html>
点击查看代码
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 ResponseTest extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
System.out.println(username+":"+password);
resp.sendRedirect("/Red");
}
}
点击查看代码
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class RedirectServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// resp.setHeader("Location","/response");
// resp.setStatus(302);
// resp.sendRedirect("/response");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html");
resp.getWriter().println("登录成功");
}
}