title: Cant pass data from ajax to java
body: <p>I need to use login_role value in UserDao.java . I tried to pass <code>$('#login_role').val()</code> to <code>session.setAttribute("type", type)</code> (so to typer variable) and get value from <code>UserDao.java</code> file. But i couldnt change <code>$('#login_role').val()</code> type to String. So i tried to pass <code>login_role</code> value by ajax to java file. But still there some problem. Maybe someone can help me on that?</p>
  
  <h3>Login.jsp</h3>
  
  <pre><code>&lt;script type="text/javascript"&gt;
              $(function () {
                  //setup before functions
                  var typingTimer;                //timer identifier
                  var doneTypingInterval = 500;   //time in ms
                  var $input = $('#login_email');
  
                  //on keyup, start the countdown
                  $input.on('keyup', function () {
                      clearTimeout(typingTimer);
                      typingTimer = setTimeout(doneTyping, doneTypingInterval);
                  });
  
                  //on keydown, clear the countdown
                  $input.on('keydown', function () {
                      clearTimeout(typingTimer);
                  });
              });
  
              //user is "finished typing"
              function doneTyping() {
                  $.ajax({
                      url: "ajax/log_in.jsp",
                      data: {
                          email: $("#login_email").val(),
                          typer: $('#login_role').val()
                      },
                      success: function (data) {
                          $("#log_in").html(data);
                      },
                      error: function (data) {
                      }
                  });
              }
  
              function loginPanel() {
  
                  var login_email = $('#login_email').val();
                  var login_password = $('#login_password').val();
  
                  if (login_email.length === 0 &amp;&amp; login_password.length === 0) {
                      alert('Write your email or password');
                      return false;
                  } else if (login_email.length === 0) {
                      alert('Write your email.');
                      return false;
                  } else if (login_password.length === 0) {
                      alert('Write your password.');
                      return false;
                  } else {
                      $.ajax({
                          url: "UserDao",
                          data: {
                              login_email: $("#login_email").val(),
                              login_password: $("#login_password:").val(),
                              typer: $('#typer').val()
                          },
                          success: function (data) {
                              alert("success");
                          },
                          error: function (data) {
                              alert("error");
                          }
                      });
                  }
              }
          &lt;/script&gt;
  
  &lt;form action="UserDao" method="post"&gt;
                      &lt;div class="form-group has-feedback"&gt;
                          &lt;input type="email" name="login_email" id="login_email" class="form-control" placeholder="Email" /&gt;
                          &lt;span class="glyphicon glyphicon-envelope form-control-feedback"&gt;&lt;/span&gt;
                      &lt;/div&gt;
                      &lt;div class="form-group has-feedback"&gt;
                          &lt;input type="password" name="login_password" id="login_password" class="form-control" placeholder="Password" /&gt;
                          &lt;span class="glyphicon glyphicon-lock form-control-feedback"&gt;&lt;/span&gt;
                      &lt;/div&gt;
                      &lt;div id="log_in"&gt;
                      &lt;/div&gt;&lt;!-- log_in --&gt;
                      &lt;div class="row"&gt;
                          &lt;div class="col-xs-8"&gt;
                              &lt;div class="checkbox icheck"&gt;
                                  &lt;label&gt;
                                      &lt;input type="checkbox" value="remember-me" id="remember_me"&gt; Remember Me
                                  &lt;/label&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;&lt;!-- /.col --&gt;
                          &lt;div class="col-xs-4"&gt;
                              &lt;input type="hidden" id="typer" name="typer"&gt;
                              &lt;button name="btnLogin" onclick="loginPanel()" class="btn btn-primary btn-block btn-flat"&gt;Sign In&lt;/button&gt;
                          &lt;/div&gt;&lt;!-- /.col --&gt;
                      &lt;/div&gt;
                  &lt;/form&gt;
  </code></pre>
  
  <h3>Log_in.jsp</h3>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      $(function () {
          //setup before functions
          var typingTimer;                //timer identifier
          var doneTypingInterval = 500;   //time in ms
          var $input = $('#login_email');
  
          //on keyup, start the countdown
          $input.on('keyup', function () {
              clearTimeout(typingTimer);
              typingTimer = setTimeout(doneTyping, doneTypingInterval);
          });
  
          //on keydown, clear the countdown
          $input.on('keydown', function () {
              clearTimeout(typingTimer);
          });
      });
  
      //user is "finished typing"
      function doneTyping() {
          $.ajax({
              url: "ajax/log_in.jsp",
              data: {
                  email: $("#login_email").val(),
                  typer: $('#login_role').val()
              },
              success: function (data) {
                  $("#log_in").html(data);
              },
              error: function (data) {
              }
          });
      }
  &lt;/script&gt;
  &lt;%  int type = -1;
      if (!request.getParameter("email").trim().equals("")) {
          type = Dao.checkUserType(request.getParameter("email"));
          if (type &gt;= 0) {%&gt;
  &lt;div id="log_in"&gt;
      &lt;% if (type == 1) { %&gt;
      &lt;div class="form-group has-feedback"&gt;
          &lt;select class="form-control" name="login_role" id="login_role"&gt;
              &lt;option value="1"&gt;Dean&lt;/option&gt;
              &lt;option value="0"&gt;Staff&lt;/option&gt;
          &lt;/select&gt;
      &lt;/div&gt;
      &lt;% } else if (type == 2) { %&gt;
      &lt;div class="form-group has-feedback"&gt;
          &lt;select class="form-control" name="login_role" id="login_role"&gt;
              &lt;option value="2"&gt;Head of Department&lt;/option&gt;
              &lt;option value="1"&gt;Dean&lt;/option&gt;
              &lt;option value="0"&gt;Staff&lt;/option&gt;
          &lt;/select&gt;
      &lt;/div&gt;
      &lt;% } else if (type == 3) { %&gt;
      &lt;div class="form-group has-feedback"&gt;
          &lt;select class="form-control" name="login_role" id="login_role"&gt;
              &lt;option value="3"&gt;Admin&lt;/option&gt;
              &lt;option value="2"&gt;Head of Department&lt;/option&gt;
              &lt;option value="1"&gt;Dean&lt;/option&gt;
              &lt;option value="0"&gt;Staff&lt;/option&gt;
          &lt;/select&gt;
      &lt;/div&gt;
      &lt;% } else {
              session.setAttribute("typer", 0);
          }%&gt;
  &lt;/div&gt;
  &lt;%
  } else {
  %&gt;
  &lt;div id="log_in"&gt;
  &lt;/div&gt;
  &lt;%  }
  } else { %&gt;
  &lt;div id="log_in"&gt;
  &lt;/div&gt;
  &lt;%  }%&gt;
  </code></pre>
  
  <h3>UserDao.java</h3>
  
  <pre><code>protected void processRequest(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
          response.setContentType("text/html;charset=UTF-8");
          try (PrintWriter out = response.getWriter()) {
              HttpSession session = request.getSession(true);
  
              if (session.getAttribute("typer") == null || "".equals(session.getAttribute("typer"))) {
                  System.out.println("typer is null");
              } else {
                  System.out.println(session.getAttribute("typer"));
              }
  
              /* TODO output your page here. You may use following sample code. */
          }
      }
  </code></pre>