title: Autocomplete using jsp and scriplets(Not using java classes)
body: <p>I want to make an autocomplete but not by using any java method. Just by using jsp, jquery, ajax and scriplets. Is it possible? Please suggest.</p>
  
  <p>I tried using the code given below, but am getting error</p>
  
  <p>Uncaught Error: cannot call methods on autocomplete prior to initialization; attempted to call method 'List.jsp'</p>
  
  <p>The code is given below:</p>
  
  <p>//(Index.jsp)</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  
  &lt;script src = "https://code.jquery.com/jquery-1.10.2.js"&gt;&lt;/script&gt;
  &lt;script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"&gt;&lt;/script&gt;
  &lt;script&gt;
  $(document).ready(function() {
  $('#country').focusin(function() {
  $("#country").autocomplete("List.jsp");
  });
  });
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;br&gt;&lt;br&gt;&lt;center&gt;
          &lt;font face="verdana" size="2"&gt;
          &lt;font size="4"&gt;Java(jsp)/jQuery Autocompleter Example&lt;/font&gt;
          &lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;
  
          Select Country   :
          &lt;input type="text" id="country" name="country"/&gt;
  
          &lt;/font&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>//(List.jsp)</p>
  
  <pre><code>&lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;
  &lt;%@page import="java.sql.*"%&gt;
  &lt;%@page import="java.util.*"%&gt;
  
  &lt;%
      try {
          String s[] = null;
  
          Class.forName("com.mysql.jdbc.Driver");
          System.out.println("Driver found");
          String url = "jdbc:mysql://localhost:protNum/dbName";
          String user = "";
          String pass = "";
          System.out.println("Connected....");
          Connection con = (Connection) DriverManager.getConnection(url, user, pass);
          Statement st = con.createStatement();
          ResultSet rs = st.executeQuery("select * from tbctry");
  
          List li = new ArrayList();
  
          while (rs.next()) {
              li.add(rs.getString(1));
          }
  
          String[] str = new String[li.size()];
          Iterator it = li.iterator();
  
          int i = 0;
          while (it.hasNext()) {
              String p = (String) it.next();
              str[i] = p;
              i++;
          }
  
          //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())) {
                  out.print(str[j] + "\n");
                  if (cnt &gt;= 5)// 5=How many results have to show while we are typing(auto suggestions)
                  {
                      break;
                  }
                  cnt++;
              }
          }
   //jQuery related end
  
          rs.close();
          st.close();
          con.close();
  
      } catch (Exception e) {
          e.printStackTrace();
      }
  %&gt;
  </code></pre>
  
  <p>I want the textbox to autopopulate values from database on keydown. I have done this using spring MVC, but I'm not getting any clue how to do this without using any java class but by just using jsp, jquery, ajax, and scriplets. Please help!</p>