【问题标题】:Why am I getting this error using if statement in jsp?为什么在jsp中使用if语句会出现这个错误?
【发布时间】:2016-09-27 04:17:02
【问题描述】:

这是我的 LoginServlet 类的代码。

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    String userName = request.getParameter("username");
    String password = request.getParameter("password");

    boolean loginFailed;

    User user = null;

    user = loginService.getUser(userName);

    if(user != null && user.getPassword().equals(password)){

        userType = loginService.getUserType(user).getUser_type();

        if(userType.equals(USER_TYPE_CUSTOMER)){
            HttpSession session = request.getSession();
            session.setAttribute("username", user.getUsername());
            response.sendRedirect("home_customer.jsp");
        }else{

        }
    }else{
        loginFailed = true;

        request.setAttribute("username", userName);
        request.setAttribute("loginFailed", loginFailed);
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/login.jsp");
        dispatcher.forward(request, response);
    }
}

我有一个名为 loginFailed 的布尔值。当用户没有帐户时,它将给出一个值 true。我在我的 jsp 页面中编写了一个代码来检查这个变量。这是我的jsp代码。

<body>
<div class="container container-table" id="login">
    <div class="row vertical-center-row">
        <div class="col-md-4 col-md-offset-4">
            <div style="background-color: #297f56; padding: 15px; border-radius: 5%;">
                <h1 style="text-align: center; color: white;">JJ STORE</h1>
                <% Boolean loginFailed = (Boolean) request.getAttribute("loginFailed");
                   if(loginFailed){
                       out.println("<div class='alert alert-warning'>Warning! Dont submit this.</div>");
                   }%>
                <form action="processLogin" method="post">
                <div class="form-group" >
                    <label for="email" style="color: white;">Username:</label>
                    <input type="text" class="form-control" name="username"  value="${username}">
                </div>
                <div class="form-group">
                    <label for="pwd" style="color: white;">Password:</label>
                    <input type="password" class="form-control" name="password">
                </div>
                <button type="submit" class="btn btn-default">Submit</button>
                </form>
            </div>
        </div>
    </div>
</div>

我不确定这里有什么问题。我尝试通过输入“true”来更改 if 块中的条件,并且代码运行顺利。但是,如果我输入从 servlet 获得的布尔值,它会给我这个错误。

enter image description here

我做错了什么?

【问题讨论】:

    标签: java html jsp servlets


    【解决方案1】:

    您仅在失败时设置loginFailed。当它成功时,你没有设置属性,所以你在 JSP 中得到一个空值,当你调用 if(loginFailed) 时会产生一个 NullPointerException

    解决问题的一种方法是在不拆箱的情况下测试Boolean 对象:

    if (Boolean.TRUE.equals(loginFailed))
    

    另一种选择是确保设置属性是成功还是失败:

    if (user != null && user.getPassword().equals(password)) {
        //...
        loginFailed = false;
    } else {
        //...
        loginFailed = true;
    }
    request.setAttribute("loginFailed", loginFailed);
    

    【讨论】:

    • 您的第一个建议解决了我的问题!这是我第一次遇到这种情况。顺便说一句,我是一名实习 jr 软件工程师,给我的项目之一是以原始方式创建网站(使用 servlet 和 jsp)。我不明白为什么我必须使用布尔值而不是布尔值(我从这里的另一个答案中得到了这个)。无论如何,谢谢哥们!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多