0

I am fetching username and password from semnatic ui modal login form (home.jsp) in servlet. Servlet will verify the credentials and return result to home.jsp the error should be displayed in the same modal. The error message is not displayed in first run in the modal it gets displayed when I again click on login button which opens this modal. I want this error message in first run. Please suggest how to achieve this. Below is the complete code.

FileBug.java (Servlet)

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

        Bugzilla bugzilla=Bugzilla.getBugzillaInstance("username", "pass");
    try {
            //some code
    }catch(Exception e)
    {
         request.setAttribute("errorMessage",e.getMessage());
            RequestDispatcher dispatcher = request.getRequestDispatcher("/home.jsp");
            dispatcher.forward(request, response);
    }
    }

home.jsp

<a class="item agenda-item" id="test">  
    <span class="side-agenda-item"> <i class="bug icon"></i> File a Bug</span>
    <div class="ui modal test">
      <form action="FileBug" method="post">
      <div class="ui segments">
       <br>
          <h3 align="center" class="header">Bugzilla Login</h3>
          <div class="ui container">
          <c:if test="${not empty errorMessage}">
          <c:out value="${errorMessage}"/>
          </c:if>
          </div>
        <div class="ui segment">

       <div class="ui input">
        <input type="text" name="username" placeholder="Bugzilla Username" required>
       </div>
      </div>
      <div class="ui segment">  
        <div class="ui input">
        <input type="password" name="password" placeholder="Bugzilla Password" required>
        </div>
      </div>

      <div class="ui segment">
      <br>
        <button class="ui blue button" id="login">Login</button>
      <br><br>
      </div>
      </div>
      </form>
    </div>
  </a>

script:-

<script>
    $(function(){
      $("#test").click(function(){
        $(".test").modal('show');
      });
      $(".test").modal({
        closable: true
      });
    });
      </script>
      <style>
        div.ui.modal{
          width: 20%;
          top: 45%;
         left: 75%;
        }
          div.ui.input {
         width: 100%;

      }
    button#login{
        margin: -20px -50px; 
        position:relative;
        top:50%; 
        left:53%;

      }
      div.ui.input input {
          display: block;
          margin: 0 auto !important;
          float: none;
      }
          </style>
Snehal Gupta
  • 314
  • 1
  • 2
  • 13

1 Answers1

0

The error message is not displayed in first run in the modal it gets displayed when I again click on login button which opens this modal.

Because in the first run, your errorMessage variable is not set. Only after you submit the form is the errorMessage variable not empty.

If you want to view errorMessage on the first run, then you need to set it before accessing home.jsp

Jonathan Laliberte
  • 2,672
  • 4
  • 19
  • 44
  • But I need to show error message only after verifying username and password provided by user i.e. after submit then how can I set it before accessing home.jsp? Please can you explain which part of code to modify. – Snehal Gupta Sep 09 '17 at 07:02
  • Yes... So remove the form from the modal. When the user submits the form, it sends the details to the servlet and then you forward the request variables back to home.jsp. You can also change doPost to doGet and then simply type this in the url: /FileBug – Jonathan Laliberte Sep 09 '17 at 09:24