尾张的织田木瓜
2021-10-26 18:12
采纳率: 100%
浏览 43
已结题

request.getParameter()获取input的值为null

浏览器输入后台能够获取user_name的值但是user_pwd的值显示为null

package edu.zut.controller;

import edu.zut.dao.UserDao;

import javax.servlet.RequestDispatcher;
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 UserLoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        String user_name = request.getParameter("user_name");
        String user_pwd = request.getParameter("user_pwd");
        System.out.println("user_name = " + user_name);
        System.out.println("user_psw = " + user_pwd);
        String error;

        if (null ==  user_name || user_name.trim().length() == 0 || null == user_pwd || 0 == user_pwd.trim().length()){

            error = "姓名或密码不能为空";
            request.setAttribute("error",error);

            RequestDispatcher dispatcher = request.getRequestDispatcher("/loginPage");
            dispatcher.forward(request, response);
        }else {

            UserDao userDao = new UserDao();
            boolean result = userDao.userLogin(user_name, user_pwd);
            System.out.println(result);
            if (result){

                request.setAttribute("user_name",user_name);
                request.getRequestDispatcher("/indexPage").forward(request,response);
            }else {
                error = "用户名或密码不正确";
                request.setAttribute("error",error);
                request.getRequestDispatcher("/loginPage").forward(request,response);
            }

        }


    }
}

登录界面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
</head>
<body>
    <form action="../userLogin" method="get">
        <input type="text" name="user_name" placeholder="请输入用户名"><br>
        <input type="password" name="user_pwd" placeholder="请输入密码"><br>
        <input type="submit" value="登录">
    </form>
</body>
</html>

img

img

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • CSDN专家-showbo 2021-10-26 20:47
    已采纳

    网址上get传值的名称是user_psw,但是代码中是user_pwd,肯定获取不到了。

    img

    题主这个url是手动打的?

    要不提交表单的话input的name是user_pwd,怎么url上的参数变成user_psw了?

    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题