@WebServlet(name = "loginServlet",urlPatterns = "/loginServlet")
public class LoginServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println(req.getParameter("username"));
System.out.println(req.getParameter("password"));
ServletContext servletContext = getServletContext();
String paw = (String)servletContext.getAttribute(req.getParameter("username"));
if(paw == null){
resp.sendRedirect("/Day55/loginfalse.html");
}else {
if ( req.getParameter("password").equals(paw))
resp.sendRedirect("/Day55/logintrue.html");
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
}
@WebServlet(name = "registerServlet",urlPatterns = "/registerServlet")
public class RegisterServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println(req.getParameter("username"));
System.out.println(req.getParameter("password"));
ServletContext servletContext = getServletContext();
if(servletContext.getAttribute(req.getParameter("username")) == null){
servletContext.setAttribute(req.getParameter("username"),req.getParameter("password"));
resp.sendRedirect("/Day55/registertrue.html");
}else {
resp.sendRedirect("/Day55/registerfalse.html");
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<form action="/Day55/loginServlet" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<button type="submit">登录</button>
</form>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<form action="/Day55/registerServlet" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<button type="submit">注册</button>
</form>
</body>
</html>