title: How to populate selectbox on page load event in using jsp and ajax
body: <p>i want to populate select box on page load but its not working</p>
  
  <p>here is my html code file1</p>
  
  <pre><code>&lt;button id="edit-btn" onclick="popupwindow('edituser.jsp','Add new user',500,300)"&gt;Edit user&lt;/button&gt;
  </code></pre>
  
  <p>file2</p>
  
  <pre><code>&lt;script&gt;
     $(document).ready(function(){
  
            $.ajax({
                          type: "POST",
                          url: "${pageContext.request.contextPath}/MyServlet",
                          data: "action=list",
                          success: function(data){ 
                            alert("success");
                          },
                          error: function(jqXHR, textStatus, errorThrown) { 
                              alert(errorThrown)
                          }
            });
     }); 
    &lt;/script&gt;
  
  &lt;select name="currentusername" id="current-username" style="width:157px;"&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p>MYDAO</p>
  
  <pre><code>  public List getAllUserNames(){       
      List&lt;UserBean&gt; users = new ArrayList&lt;UserBean&gt;();
      try {
          String sql = "SELECT username FROM users ";
          PreparedStatement ps = connection.prepareStatement(sql);
          ResultSet rs = ps.executeQuery();
          if (rs != null) {
          while (rs.next()) {
               UserBean userBean = new UserBean();
               System.out.println(rs.getString("username"));
               userBean.setUsername(rs.getString("username"));
  
               users.add(userBean);
          }
          }
          else{
              System.out.println("No users");
          }
      } catch (SQLException e) {
          e.printStackTrace();
      }
      return users;            
  }
  </code></pre>
  
  <p>Servlet</p>
  
  <pre><code> if(action.equalsIgnoreCase("list")){
           System.out.println("list");
           request.setAttribute("users", dao.getAllUserNames());          
      }
  </code></pre>