简体   繁体   English

使用 servlet 在登录页面本身显示密码错误消息

[英]Display password error message in login page itself using servlet

I want to display Username or password incorrect message in a particular <div> in the Login page.我想在登录页面的特定<div>中显示用户名或密码错误消息。

Here is my login.html code.这是我的 login.html 代码。

<div class="wrapper">
            <form class="form-signin" method="POST" action="j_security_check">       
                <h2 class="form-signin-heading text-center" id="head">Login</h2>
                <input type="text" class="form-control"  id="txtuser" name="j_username" placeholder="Username" required="required" />
            <input type="password" class="form-control" id="txtuser" name="j_password" placeholder="Password" required="required"/>
            <div id="error"></div>  
                <button class="btn btn-lg btn-default btn-block login1" id="txtuser" type="submit"><span class="glyphicon glyphicon-log-in"></span> Login</button>   
            </form>
</div>

Here is my servlet code这是我的 servlet 代码

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

  response.setContentType("text/html");
  PrintWriter out = response.getWriter();

  RequestDispatcher rd=request.getRequestDispatcher("/login.html");

  out.println("<script>");
  out.println("document.getElementById('error').innerText=Sorry UserName or Password Error");
  out.println("</script>");

  rd.include(request, response);
}

The message is not displaying in the <div id="error"> in login file该消息未显示在登录文件的<div id="error">

I found an answer.It is working我找到了答案。它正在工作

Here is my JSP这是我的 JSP

<%
String login_msg=(String)request.getAttribute("error");  
if(login_msg!=null)
out.println("<font color=red size=4px>"+login_msg+"</font>");
%>

Here is my Servlet这是我的 Servlet

request.setAttribute("error","Invalid Username or Password");
RequestDispatcher rd=request.getRequestDispatcher("/login.jsp");            
rd.include(request, response);

Just set the error message in request as an attribute and show the error div if it's not null.只需将请求中的错误消息设置为属性,如果不为空则显示错误 div。

Servlet:小服务程序:

request.setAttribute("error","Invalid Username/password");

Login.jsp:登录.jsp:

Use JSTL and EL for accessing request attribute in JSP.使用 JSTL 和 EL 访问 JSP 中的请求属性。 You can use<c:if> for testing the error message.您可以使用<c:if>来测试错误消息。

<c:if test="${not empty requestScope.error}">
      <!-- Show the error div with message-->
</c:if>

Use Implicit object requestScope to get the attribute from request scope.使用隐式对象requestScope从请求范围获取属性。


If you want to use HTML only then try it.如果您只想使用 HTML,请尝试一下。 Not a good way but you can create two separate HTML pages for login.html and login_error.html then redirect to login_error.html when there is any error.这不是一个好方法,但您可以为 login.html 和 login_error.html 创建两个单独的 HTML 页面,然后在出现任何错误时重定向到 login_error.html。

request.getRequestDispatcher("/login_error.html");

Try this:尝试这个:

request.getRequestDispatcher("signin.html").include(request, response);
out.println("<script>document.getElementById('error').innerHTML='Sorry UserName or Password'; </script>");

Don't forget to use single-quotes for string.不要忘记对字符串使用单引号。

Simply move只需移动

rd.include(request, response);

before your print statements and it should work.在您的打印语句之前,它应该可以工作。 It is because you want the script to execute after the div is defined.这是因为您希望脚本在定义 div 后执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Spring 安全性在 login.jsp 页面上显示登录错误消息? - How to display login error message at login.jsp page using Spring Security? 用户在JSP和Servlet中无法使用Session登录时出现错误消息 - Error message when user Fails to Login using Session in JSP and Servlet 如何在github登录页面上提取错误消息``用户名或密码错误&#39;&#39; - How to extract the error message 'Incorrect username or password' on github login page 错误页面未显示在servlet中 - error page did not display in servlet 使用servlet在Facebook上登录时出错 - error in login on facebook using servlet 登录ajax jsp servlet:在div中加载欢迎页面,保留此错误消息 - Login ajax jsp servlet: Welcome page loading in div reserved for error message Servlet重定向到同一页面,并显示错误消息 - Servlet redirect to same page with error message 登录页面的安全性(使用Servlet / JSP) - Security for the login page (using Servlet/JSP) 如何在不使用javascript的情况下在html页面中显示不正确的密码错误 - How to Display incorrect password error in html page without using javascript 条件身份验证:如何在页面servlet上有条件地显示登录 - Conditional Authentication: How to conditionally display login on page servlet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM