title: call jsp for autosuggestion search box by selecting column of database in dropdown list
body: <p>I want a autosuggestion searchbox which search things from database.I have a feature of selecting by which column user search bu providing dropdownlist of database columns.For one column its working fine.But as I select another column,it search both the first selected column and the second just selected column.
  I want not so.Make me out of it
  Here is is code.It has mvc design pattern.<br/>
  <strong>Index.html</strong><br/></p>
  
  <pre><code>  &lt;script type="text/javascript"&gt;
      $(function(){
      $("#country").autocomplete("find.me", {
            extraParams: {
              choice: function() { 
                return $("#searchby").val();
              }
            }
          });
  });
      &lt;/script&gt;
         &lt;div&gt;
  
  
   Search By:
    &lt;select name="searchby" id="searchby" &gt;
    &lt;option disabled selected value&gt; -- select an option -- &lt;/option&gt;
    &lt;option value="name"&gt;Name&lt;/option&gt;
    &lt;option value="roll_no"&gt;Roll No&lt;/option&gt;
    &lt;option value="city"&gt;City&lt;/option&gt;
        &lt;option value="pin_code"&gt;Pin Code&lt;/option&gt;
        &lt;option value="state"&gt;State&lt;/option&gt;
        &lt;option value="country"&gt;Country&lt;/option&gt;
        &lt;/select&gt;
       &lt;/div&gt;
          &lt;span class="input input--nariko"&gt;
  
            &lt;input id="country" class="input__field input__field--nariko" type="text" id="input-20" name="country" /&gt;
            &lt;label class="input__label input__label--nariko" for="input-20"&gt;
              &lt;span class="input__label-content input__label-content--nariko"&gt;Type Something&lt;/span&gt;
            &lt;/label&gt;
          &lt;/span&gt;
  </code></pre>
  
  <p><br/><strong>Servlet Controller</strong> <br/>   </p>
  
  <pre><code>    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      // TODO Auto-generated method stub
      PrintWriter out=response.getWriter();
      String searchBy=request.getParameter("choice");
  
      String[] arr=new AutoCompleteDao().autoComplete(searchBy);
  
   request.setAttribute("arr", arr);
      RequestDispatcher requestDispatcher=request.getRequestDispatcher("list.jsp");
      requestDispatcher.forward(request, response);
  }
  </code></pre>
  
  <p><br/><strong>Dao Class AutocompleteDao.java</strong><br/>
       public class AutoCompleteDao {
      private static final String DRIVER_NAME="com.mysql.jdbc.Driver";
      private static final String URL="jdbc:mysql://localhost:3306/";
      private static final String DB_NAME="autotest";</p>
  
  <pre><code>private static final String USER_NAME="root";
  private static final String PASSWORD="WELcome@123";
  Connection connection;
  
  
  public String[] autoComplete(String searchBy){
      PreparedStatement stmt=null;
      ResultSet resultSet=null;
  
      String query=null;
      try{
          //Register JDBC Driver
          Class.forName(DRIVER_NAME).newInstance();
          //open a connection
          connection=(Connection)DriverManager.getConnection(URL+DB_NAME,USER_NAME,PASSWORD);
            switch(searchBy){
             case "name":
                query= "SELECT name FROM test";
                 break;
             case "country":
                 query="SELECT country FROM test";
                 break;
             }
  
          stmt=(PreparedStatement) connection.prepareStatement(query);
  
       resultSet= stmt.executeQuery();
         List li = new ArrayList();
  
  
  
  
         while(resultSet.next())
         {
             li.add(resultSet.getString(1));
         }
  
  
      }
      catch(SQLException e){
          e.printStackTrace();
      }
      catch(ClassNotFoundException e){
          e.printStackTrace();
      }
      catch(InstantiationException e){
          e.printStackTrace();
      }
      catch(IllegalAccessException e){
          e.printStackTrace();
      }
      finally {
          try{
              if(stmt!=null)
                  stmt.close();
              if(connection!=null)
                  connection.close();
          }
          catch(SQLException e){
              e.printStackTrace();
          }
      }
  
  return li.toArray(new String[li.size()]);
  }
  </code></pre>
  
  <p><br/><strong>Web.xml</strong><br/></p>
  
  <pre><code>  &lt;servlet&gt;
  &lt;servlet-name&gt;AutoCompleteServlet&lt;/servlet-name&gt;
    &lt;servletclass&gt;AutoCompleteServlet&lt;/servlet-class&gt;
    &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
    &lt;servlet-name&gt;AutoCompleteServlet&lt;/servlet-name&gt;
    &lt;url-pattern&gt;/find.me&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p><br/>And list.jsp which is displaying result<br/>
  <strong>List.jsp</strong><br/></p>
  
  <pre><code>   &lt;%String[] str=null;
  str=(String[])request.getAttribute("arr"); 
  if(str!=null){
  //jQuery related start
  String query = (String)request.getParameter("q");
  
  int cnt=1;
  for(int j=0;j&lt;str.length;j++)
  {
      if(str[j].toUpperCase().startsWith(query.toUpperCase()))
      {%&gt;
  
         &lt;%=str[j] %&gt;
  
  &lt;%    } 
  }
  str=null;
  //jQuery related end
  }
  %&gt;
  </code></pre>
  
  <p>Here when i select name from dropdownlist and type in autosuggesstion it shows name from database when there is a match found.But when I select country.
  It shows i.e. suggest both country and name from database.
  I really cant understand why?</p>