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>
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);
}
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
Since you are already using scriptlet in your jsp, you can use:
<%
if(null!=request.getAttribute("errorMessage"))
{
out.println(request.getAttribute("errorMessage"));
}
%>
It would be better if you go for JSTL:
<c:if test="${not empty errorMessage}">
<c:out value="${errorMessage}"/>
</c:if>
To learn more about those ${} things (the Expression Language, which is a separate subject from JSTL), check here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With