title: How to populate a droplist with data from database using jsp,servlet,ajax,jquery
body: <p>Friends.
  I want to populate the search box which is like a dropdown list which will populate dynamically when something is typed into it with user's full name which is made up firstname and lastname.
  The jsp form has this search box(drop down list) . I am using ajax so that I can show the ArrayList which contains all the user's full name dynamically as the user enters the name in search box. 
  I am totally new to ajax or jquery. Please guide me for passing the arrayList data from the servlet as response to ajax or jquery script and populate the dropdown asynchronously. 
  Please help </p>
  
  <p>Here is my servlet code </p>
  
  <pre><code>@WebServlet("/someservlet/*")
  public class AjaxExampleServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;
  
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
      //Database connection
  
       try {
          Connection currentCon = ConnectionManager.getConnection();
           currentCon.setAutoCommit(true);
          ResultSet rs;
           Statement stmt = null;
           stmt = currentCon.createStatement();
  
           //create the query
  
           String query = "select firstname,lastname,username from users";
  
          //executing the query
           rs = stmt.executeQuery(query);
  
          //creating a hashmap
  
           ArrayList&lt;String&gt; UserFullName = new ArrayList&lt;String&gt;();
  
          //fill the hashmap UserFullName with firstname and lastname of the user fetched from the database
          while(rs.next())
          {
              UserFullName.add(rs.getString("firstname") + rs.getString("lastname"));
          }
  
          response.setContentType("text/plain");  // Set content type of the response so that jQuery knows what it can expect.
          response.setCharacterEncoding("UTF-8");
          request.setAttribute("list", UserFullName);
  
          RequestDispatcher requestDispatcher = request.getRequestDispatcher("response.jsp");
          requestDispatcher.forward(request, response);
  </code></pre>
  
  <p>and my first jsp page</p>
  
  <pre><code>  &lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"     pageEncoding="ISO-8859-1"%&gt;
    &lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %&gt;
   &lt;!DOCTYPE html&gt;
   &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;&lt;/title&gt;
      &lt;script src="http://code.jquery.com/jquery-latest.min.js"&gt;&lt;/script&gt;
      &lt;script type="text/javascript"&gt;
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form action="someservlet" method="get"&gt;
      &lt;button type="submit"  id="somebutton"&gt;press here&lt;/button&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>and my response.jsp</p>
  
  <pre><code>  &lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%&gt;
   &lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %&gt;
  
   &lt;!DOCTYPE html&gt;
   &lt;html&gt;
   &lt;head&gt;
   &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
   &lt;title&gt;Insert title here&lt;/title&gt;
   &lt;/head&gt;
   &lt;body&gt;
      &lt;button id="somebutton"&gt;press here&lt;/button&gt;
      &lt;div id="somediv"&gt;
      &lt;label for="users"&gt;Users&lt;/label&gt;
          &lt;select id="users" multiple="multiple"&gt;
          &lt;c:forEach var="listvar" items="${list}"&gt;
  
                  &lt;option value="${listvar}"&gt;${listvar}&lt;/option&gt;
  
          &lt;/c:forEach&gt; 
          &lt;/select&gt;
      &lt;/div&gt;
   &lt;/body&gt;
   &lt;/html&gt;
  </code></pre>
  
  <p>I want my response in the same jsp from first jsp page where the users where searched directly into the search dropdown list using ajax,jquery.
  Examples are welcomed. Please help friends.</p>