title: Calling Servlet with jquery
body: <p>I have following requirement</p>
  
  <ol>
  <li>on Login.html page - Login btn and Forget password btn </li>
  <li>if Login btn click,</li>
  <li>username and pswd text field validation should be done.</li>
  <li>If validation success - call LoginServlet</li>
  <li>If LoginServlet validate user successfully -> call another DashboardServlet</li>
  <li>If LoginServlet validate user is failed --> return failure message to login.html page and login.html page should show alert or message.</li>
  <li>What is working? - empty text box validation working, I can find which btn is clicked login or forgot password.</li>
  <li>I am not using jsp page, so please suggest solution which works with html.</li>
  </ol>
  
  <p><strong>I am struggling to find solution from last 3 days, I have already refereed SO and other portals help taken but still lacking final output</strong></p>
  
  <p><strong>Login.html</strong></p>
  
  <pre><code>&lt;script&gt;
  function validateloginform() {
  var name = document.getElementById("username").value;
  var password = document.getElementById("password").value;
  if (name == null || name == "") {
      alert("Please enter valid userName");
      return false;
  } else if (password.length &lt; 6) {
      alert("Password must be at least 6 characters long.");
      return false;
  }else
      {return true;
      }
  }
  $(document).ready(function(){
      // Set a variable, we will fill later.
      var value = null;
  
  // On submit click, set the value
  $('input[type="submit"]').click(function(){
      value = $(this).val();
  });
  
  // Use the set value in the submit function
  $('form').submit(function (event)
  {
      event.preventDefault();
      //alert(value);
      if(value == "LOGIN")
          {
          if (validateloginform()){
              alert('i want to call LoginServlet here');
          }
      else if (value == "FORGOT PASSWORD")
          window.location.href = "ChangePassword.html"
  
  });});//ready function
  &lt;/script&gt;
  
  &lt;/head&gt;
   &lt;body background="../Images/zebra_background.jpg"&gt;
          &lt;p id="header"&gt; ZMC Server&lt;/p&gt;
          &lt;div class="combine"&gt;
          &lt;img class="logo" src="../Images/zebra_logo.png"&gt;&lt;br&gt;
          &lt;text&gt;Welcome To&lt;/text&gt;
          &lt;text1&gt;Admin Console Login Page &lt;/text1&gt;
     &lt;!-- onsubmit="return validateloginform();" --&gt;
      &lt;form action="/zmcwebadmin/loginServlet" method="post" 
           id="loginForm" name="loginform"&gt; &lt;!-- &lt;/form&gt; --&gt;
  
          &lt;div class="usernameformat"&gt;
              &lt;input type="text" name="username" class="userformat" id="username"
                  placeholder="User Name"&gt; &lt;br&gt; &lt;input type="password"
                  name="password" id="password" class="passformat" placeholder="Password"&gt;
          &lt;/div&gt;
  
          &lt;p class="rememberme"&gt;
              Remember me &lt;img id="remember_me" onclick="changeRememberImage()"
                  src="../Images/remember_me_off.png" width="28" height="20"
                  style="float: right"&gt;
          &lt;/p&gt;&lt;br&gt;
  
          &lt;input type="submit" class="buttonformat" value="LOGIN" id="btnlogin" name="btnlogin"&gt;
  
           &lt;input
              type="submit" class="buttonformat" value="FORGOT PASSWORD" id="btnforgetpwd" name="btnforgetpwd"&gt;
      &lt;/form&gt;
  </code></pre>
  
  <p><br>
    
    </p>
  
  <p><strong>LoginServlet</strong></p>
  
  <pre><code>/**
  * Servlet implementation class LoginServlet
  */
  @WebServlet("/loginServlet")
  public class LoginServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;
  
  /**
   * @see HttpServlet#HttpServlet()
   */
  public LoginServlet() {
      super();
      // TODO Auto-generated constructor stub
  }
  
  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      System.out.println("I am in doGet of LoginServlet");
      response.sendRedirect("/html/Loginpage.html");
  }
  /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      System.out.println("I am in doPost of LoginServlet");
      String userName = request.getParameter("username");
      String passWord = request.getParameter("password");
  
      if(LoginDao.validate(userName, passWord))
      {
          HttpSession session=request.getSession();  
          if(session.isNew())
             {
              //New session creation
                session=request.getSession(true);
             }
              else
              {
                 session = request.getSession(false);
              }
  
           //setting attribute on session
           session.setAttribute("userName",userName);
           //send request to Welcome.jsp page
           //response.sendRedirect("/Dashboard.html");
           response.sendRedirect("/zmcwebadmin/dashBoardServlet");
           response.getWriter().write(String.valueOf(userName));
           System.out.println("I have connected to DB");
      }
  
      else
      {
          //response.sendRedirect("error.html");
          String data = "User name and password do not match";
          response.setContentType("text/plain");
          response.setCharacterEncoding("UTF-8");
          response.getWriter().write(data);
          /*RequestDispatcher rd = request.getRequestDispatcher("Loginpage.html"); -- This solution works
          rd.forward(request, response);*/
          //response.sendRedirect("Loginpage.html");
  
          request.setAttribute("error", "Unknown login, try again");
          doGet(request, response);
  
          System.out.println("I have not connected to server");
      }
   }
  }
  </code></pre>