title: How can i pass the selected dropdown list values from .jsp to servlet using ajax
body: <p>I am using dropdown list in my program and I am selecting dropdown list values using ajax.
  But the problem is I am unable to pass the selected dropdown values to servlet and servlet must use those values. </p>
  
  <pre><code>  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          // TODO Auto-generated method stub
          HttpSession session = request.getSession(false);
          String username=(String)session.getAttribute("uname");
          String mnth = request.getParameter("mnth");
          String yr = request.getParameter("yr");
          String mnthyr = mnth+"-"+yr;
          PrintWriter out=response.getWriter();
          Gson g=new Gson();
          out.print(g.toJson(Dao.get_attendence_by_mnthyr(username, mnthyr)));
      }
  
  }
  
  
  &lt;%@page import="db.Dao"%&gt;
  &lt;%@page import="javaprocessors.Date" %&gt;
  &lt;%@page import="java.sql.*" %&gt;
  &lt;%@page import="beans.GetBeans" %&gt;
  &lt;%@page import="java.sql.PreparedStatement" %&gt;
  &lt;%@page import="com.google.gson.Gson" %&gt;
  &lt;%@page import="db.Connect_db" %&gt;
  &lt;%@page import="java.sql.Connection" %&gt;
  &lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;link rel="stylesheet" href="resources/assets/css/table.css"&gt;
  &lt;title&gt;Insert title here&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
      &lt;div class="CSS_Table_Example" style="width:925px;"&gt;
  
  &lt;table&gt;
  &lt;thead&gt;
                  &lt;tr&gt; 
  
                      &lt;td rowspan="1" colspan="9"&gt;
  
                  &lt;strong&gt;Month&lt;/strong&gt;
  
                  &lt;select size="1" id="chng" name="mnth"&gt;
                  &lt;option value="01"&gt;January&lt;/option&gt;
                  &lt;option value="02"&gt;February&lt;/option&gt;
                  &lt;option value="03"&gt;March&lt;/option&gt;
                  &lt;option value="04"&gt;April&lt;/option&gt;
                  &lt;option value="05"&gt;May&lt;/option&gt;
                  &lt;option value="06"&gt;June&lt;/option&gt;
                  &lt;option value="07"&gt;July&lt;/option&gt;
                  &lt;option value="08"&gt;August&lt;/option&gt;
                  &lt;option value="09"&gt;September&lt;/option&gt;
                  &lt;option value="10"&gt;October&lt;/option&gt;
                  &lt;option value="11"&gt;November&lt;/option&gt;
                  &lt;option value="12"&gt;December&lt;/option&gt;
                  &lt;/select&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
                  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
  
                  &lt;strong&gt;Year&lt;/strong&gt;
  
                  &lt;%HttpSession sesion = request.getSession(false);
                      String shft = null;
                      String username=null;
                      username=(String)session.getAttribute("uname");
                      for(GetBeans g: Dao.getshift(username)){
                      shft = g.getShift();
                      }%&gt;
                  &lt;%ResultSet resultset =null;%&gt;
                  &lt;% Connection connection=Connect_db.getConn();
                      Statement statement = connection.createStatement() ; 
  
                  resultset =statement.executeQuery("SELECT DISTINCT YEAR(date) FROM tb_attendence WHERE emp_name LIKE '"+username+"';") ;%&gt;  
  
                      &lt;select size="1" id="chng" name="yr"&gt;
                      &lt;% while(resultset.next()){ %&gt; &lt;option&gt;&lt;%= resultset.getString(1)%&gt;&lt;/option&gt; &lt;% } %&gt;
  
                  &lt;/select&gt;
  
                  &lt;button class="btn btn-primary" id="sbtn1"&gt;Go&lt;/button&gt;
                  &lt;button type="submit" class="btn btn-default" id="sbtn2"&gt;Download   &lt;/button&gt;
  
                  &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td &gt;
                          Employee Name
                      &lt;/td&gt;
                      &lt;td&gt;
                              Date
                      &lt;/td&gt;
                      &lt;td&gt;
                              Duty Marked
                      &lt;/td&gt;
                      &lt;td&gt;
                              Duty Attended
                      &lt;/td&gt;
                      &lt;td&gt;
                              In Time
                      &lt;/td&gt;
                      &lt;td&gt;
                              Out Time
                      &lt;/td&gt;
                      &lt;td&gt;
                              In Time Latency
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;/thead&gt;
                      &lt;tbody id="tbdy"&gt;
  
                  &lt;%sesion = request.getSession(false);
                  username=(String)session.getAttribute("uname");
                  String mnthyr = Date.getmnthyr();
                  connection=Connect_db.getConn();
  
                  String query="select emp_name,date,duty_marked,duty_attended,login,logout,latency from tb_attendence where emp_name like '"+username+"' and DATE_FORMAT(date, \"%m-%Y\") =\""+mnthyr+"\";";
                  PreparedStatement ps=connection.prepareStatement(query);
  
  
                  ResultSet rs=ps.executeQuery();%&gt;
                  &lt;%while(rs.next()){ %&gt;`enter code here`
                  &lt;tr&gt;
  
                  &lt;td&gt; &lt;%= rs.getString(1) %&gt;&lt;/td&gt;
                  &lt;td&gt; &lt;%= rs.getString(2) %&gt;&lt;/td&gt;
                  &lt;td&gt; &lt;%= rs.getString(3) %&gt;&lt;/td&gt;
                  &lt;td&gt; &lt;%= rs.getString(4) %&gt;&lt;/td&gt;
                  &lt;td&gt; &lt;%= rs.getString(5) %&gt;&lt;/td&gt;
                  &lt;td&gt; &lt;%= rs.getString(6) %&gt;&lt;/td&gt;
                  &lt;td&gt; &lt;%= rs.getString(7) %&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;% } 
              connection.close();%&gt;
  
              &lt;/table&gt;
              &lt;/div&gt;
          &lt;/body&gt; 
  
          &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"&gt;&lt;/script&gt;
  
          &lt;script type="text/javascript"&gt;
          $(document).ready(function(){
  
              var mnth = $('#mnth :selected').val();
              var yr = $('#yr :selected').val();
              var dataString = "mnth=" + mnth + "&amp;yr=" + yr;
              $('#sbtn1').click(function(){
  
                  $('#tbdy').html('');
                  $.ajax({url: "In_Out",
                      datatype:'JSON',
                      data: dataString,
                      success: function(result){
                     var obj = JSON.parse(result);
  
  
                     for(i=0;i&lt;obj.length;i++)
                         {
                       $('#tbdy').append('&lt;tr&gt; &lt;td&gt;'+(i+1)+'&lt;/td&gt; &lt;/tr&gt;');  
                         }
  
                  }});
              });
  
          });
          &lt;/script&gt;
  &lt;/html&gt;
  </code></pre>