登录失败后在登录页面中显示错误消息 [英] displaying error message in login page after login failure

查看:427
本文介绍了登录失败后在登录页面中显示错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是login.jsp中的部分代码

here is some part of code from 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>

如果用户不存在,则在servlet代码中

我正在使用带有错误msg的requestdispatcher

in the servlet code if the user does't exist i'm using requestdispatcher with error msg

servlet

if(res.next())
        {
                if ((thisname.equals(res.getString("username"))) && (thispwd.equals(res.getString("password"))))
                {
                    session.setAttribute("username", request.getParameter("username"));
                    response.sendRedirect("login-success.html");
                }
                else{
                    session.invalidate();
                    request.setAttribute("errorMessage", "Invalid user or password");
                    RequestDispatcher rd = request.getRequestDispatcher("/login.jsp");
                    rd.forward(request, response);          
                }

但是事情是<h2><%=request.getAttribute("errorMessage") %></h2>运行,只要打开登录jsp就会在没有失败的情况下在页面上返回null.仅当失败发生时,它显示无效的用户或密码".所以我想检查request.getAttribute ("errorMessage")为null.是它不然后调用<h2><%=request.getAttribute("errorMessage") %></h2>.我该怎么做?还是有更好的方法

but thing is <h2><%=request.getAttribute("errorMessage") %></h2> runs whenever login jsp is opened which returns null on the page when there is no failure.only when the failure occurs it displays "invalid user or password" .so i want to check if request.getAttribute("errorMessage") is null . is its not then invoke <h2><%=request.getAttribute("errorMessage") %></h2>. how do i do it? or is there a better way

推荐答案

由于您已经在jsp中使用了scriptlet,因此可以使用:

Since you are already using scriptlet in your jsp, you can use:

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

如果您选择JSTL,那会更好:

It would be better if you go for JSTL:

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

要了解有关$ {}的更多信息(表达语言,它是与 JSTL ),

To learn more about those ${} things (the Expression Language, which is a separate subject from JSTL), check here.

这篇关于登录失败后在登录页面中显示错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆