Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsp 登录失败后在登录页面中显示错误消息_Jsp_Servlets - Fatal编程技术网

Jsp 登录失败后在登录页面中显示错误消息

Jsp 登录失败后在登录页面中显示错误消息,jsp,servlets,Jsp,Servlets,下面是login.jsp中的部分代码 login.jsp <h2 class="swd-postheader">Login</h2> <form method="post" action="Login.select"> <p><input id="username" name= "username" type="text" placeholder="Usernam

下面是login.jsp中的部分代码

login.jsp

         <h2 class="swd-postheader">Login</h2>
              <form method="post" action="Login.select">
                 <p><input id="username" name= "username" type="text" placeholder="Username" style="margin:10px" autofocus required></p>
                 <p> <input id="password" name="password" type="password" placeholder="Password" style="margin:10px" required></p>
                 <p><input class="swd-button" type="submit" value="Sign In">
                    <a href="register.html"><button class="swd-button" type="button">New User</button></a>
                 </p>
              </form>
              <h2><%=request.getAttribute("errorMessage") %></h2>
           </div>
        </div>

但问题是,
在登录jsp打开时运行,当没有失败时,页面上返回null。只有当失败发生时,才会显示“无效的用户或密码”。所以我想检查request.getAttribute(“errorMessage”)是否为null。它不是那么调用
。我该怎么做?或者有没有更好的方法

因为您已经在jsp中使用了scriptlet,您可以使用:

<%
    if(null!=request.getAttribute("errorMessage"))
    {
        out.println(request.getAttribute("errorMessage"));
    }
%>

如果您选择JSTL会更好:

<c:if test="${not empty errorMessage}">
   <c:out value="${errorMessage}"/>
</c:if>


要进一步了解这些${}事物(the,它是独立于的主题),。

这里的jsp示例在h2中作为静态html运行,请给出一个条件,该条件仅在提交完成后返回h2中的值。发布完整的jsp和servlet将有助于解决您的问题。谢谢Zeeshan。工作完美。正是我需要的。即使我给
null=,页面抛出空指针异常
<c:if test="${not empty errorMessage}">
   <c:out value="${errorMessage}"/>
</c:if>