代码如下,使用post提交表单时,和java代码中注释的地方(客户端跳转)相矛盾,而使用get方式提交时却可以,但是会暴漏用户信息。为什么post会失败?
JAVA代码
  @RequestMapping("/login")
    public String login(HttpServletRequest req, HttpServletResponse res){
        String password = req.getParameter("password");
        String name = req.getParameter("name");
        List<User> users = userDAO.findAll();
        for(User user1 : users){
            if(user1.getPhoneNumber().equals(name) || user1.getPetName().equals(name)){
                if(user1.getPassword().equals(password)){
                    //设置Cookie,保存用户名
                    Cookie cookie = new Cookie("username",name);
                    res.addCookie(cookie);
//                    try {
//                        req.getRequestDispatcher("setHeadImg.html").forward(req, res);
//                    } catch (ServletException e) {
//                        e.printStackTrace();
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
                    return "登录成功";
                }else{
                    return "密码错误";
                }
            }
        }
        
        //执行到这一步,说明用户提交的信息不在user表中,提示用户注册
        try {
            res.sendRedirect("register.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "账号未注册";
    }
HTML
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>登录页面</title>
</head>
<body>
<form action="login" method="post">
    昵称或手机号:<input type="text" name="name"/>
    <br/>
    密码:<input type="text" name="password"/>
    <br/>
    <input type="submit" value="登录">
</form>
</body>
</html>
        网上有个post 也是这样,说是路径的问题
https://blog.csdn.net/cherry_chenrui/article/details/77709577
改成重定向确实可以解决问题。但是post为什么不行的具体原因还是不清楚。可能要深入了解post和get。
应该是 有隐藏的字段没有提交
是指表单的隐藏字段?
可以试试改成PostMapping,(虽然requestmapping应该也是可以的)
PostMapping不行
检查下请求的content-type