title: displaying error message in login page after login failure
body: <p>here is some part of code from login.jsp</p>
  
  <p><strong>login.jsp</strong></p>
  
  <pre><code>         &lt;h2 class="swd-postheader"&gt;Login&lt;/h2&gt;
                &lt;form method="post" action="Login.select"&gt;
                   &lt;p&gt;&lt;input id="username" name= "username" type="text" placeholder="Username" style="margin:10px" autofocus required&gt;&lt;/p&gt;
                   &lt;p&gt; &lt;input id="password" name="password" type="password" placeholder="Password" style="margin:10px" required&gt;&lt;/p&gt;
                   &lt;p&gt;&lt;input class="swd-button" type="submit" value="Sign In"&gt;
                      &lt;a href="register.html"&gt;&lt;button class="swd-button" type="button"&gt;New User&lt;/button&gt;&lt;/a&gt;
                   &lt;/p&gt;
                &lt;/form&gt;
                &lt;h2&gt;&lt;%=request.getAttribute("errorMessage") %&gt;&lt;/h2&gt;
             &lt;/div&gt;
          &lt;/div&gt;
  </code></pre>
  
  <p>in the servlet code if the user does't exist i'm using requestdispatcher with error msg</p>
  
  <p><strong>servlet</strong></p>
  
  <pre><code>if(res.next())
          {
                  if ((thisname.equals(res.getString("username"))) &amp;&amp; (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);          
                  }
  </code></pre>
  
  <p>but thing is <code>&lt;h2&gt;&lt;%=request.getAttribute("errorMessage") %&gt;&lt;/h2&gt;</code> 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 <code>&lt;h2&gt;&lt;%=request.getAttribute("errorMessage") %&gt;&lt;/h2&gt;</code>. how do i do it? or is there a better way</p>