title: Return JSON object from JSP to JS
body: <p>I am trying to return JSON Object/Array from JSP to JavaScript. I dont know how to import JSP file in JS. I have populated JSON Array with DB values.</p>
  
  <p><strong>main.js:</strong></p>
  
  <pre><code>$(document).ready(function() {
          $(function() {
                  $("#search").autocomplete({     
                  source : function(request, response) {
                  $.ajax({
                          url : "operation.jsp",
                          type : "GET",
                          data : {
                                  term : request.term
                          },
                          dataType : "json",
                          success : function(data) {
                                  response(data);
                          }
                  });
          }
  });
  });
  </code></pre>
  
  <p><strong>Operation.jsp</strong>:</p>
  
  <pre><code>               try{
                      Class.forName(driverName);
                      connection = DriverManager.getConnection(connectionUrl, userId, password);
                      System.out.println("Connection Success");
                      statement = connection.createStatement();
                      String sql = "SELECT * FROM sample";
  
                      resultSet = statement.executeQuery(sql);
                      JSONArray array = new JSONArray();
                      JSONObject object = new JSONObject();
  
                      while (resultSet.next()) {
                          System.out.println(resultSet.getString("Name"));
                          System.out.println(resultSet.getString("Age"));
  
                         object.put("Name", resultSet.getString("Name"));
                          object.put("Age", resultSet.getString("Age"));
                          array.put(object);
  
                      }
                      System.out.println("The Array is" + array);
  
                      response.setContentType("application/json");
                     response.getWriter().write(array.toString());
  
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
  </code></pre>
  
  <p>I need to populate the return JSON data in HTML Dropdown box.</p>