0

I am trying to implement a simple jquery autocomplete by retrieving data from the database. But somehow the autocomplete drop down with suggestions is not displayed. It works fine when I have a hard coded array. It is only with JSP that the problem exists.

My list.jsp code.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<%@page import="java.util.*"%>

<% 
try{      
     String s[]=null;
        ResultSet rs = null;

    Class.forName("oracle.jdbc.driver.OracleDriver"); 
    Connection con =DriverManager.getConnection("jdbc:oracle:thin:@tst:1521:gtst02","tiger","tiger");
    String queryy=("SELECT ID FROM (SELECT DISTINCT NAME FROM GOOG  WHERE NUM LIKE ? ) WHERE ROWNUM<11");
    PreparedStatement st=con.prepareStatement(queryy); 
    String query = (String)request.getParameter("q");
    st.setString(1,query+"%");
    rs = st.executeQuery();
    while (rs.next()) {
        out.print(rs.getString("CONTRACT_NUM")+"\n");
        }       
    rs.close(); 
    st.close(); 
    con.close();

        } 
    catch(Exception e){ 
        e.printStackTrace(); 
    }
 %>

My html code :

<html lang="en">
<head>

<meta charset="utf-8" />


<title>jQuery UI Autocomplete - Remote datasource</title>


<link rel="stylesheet"
href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />

<style>
.ui-autocomplete-loading {
background: white url('images/ui-anim_basic_16x16.gif') right center
    no-repeat;
}
</style>
<script>
 $(function() {

     //var availableTags = [ "ActionScript","AppleScript"];
     $( "#tags" ).autocomplete({      
         source: "list.jsp"    
         });
 });
 </script>

</head>

<body>
<div class="ui-widget">  

<label for="tags">Tags: </label>  

<input id="tags" /></div>


 </body>
</html>

When I hit

http://localhost:7001/Test/list.jsp&q=206 

It displays a popup asking me whether to so the file or open (download). When I open, it displays the values in notepad with a long space.

Please let me know where I am going wrong here ? It works fine with a declared hard coded array of values.

user1722908
  • 535
  • 2
  • 9
  • 21

1 Answers1

0

Please visit: jQuery API

Look at the section where a string is passed. In short jQuery appears to pass a parameter called 'term' not 'q' and expects JSON results.

Your output of data is not in a format that is accepted by jQuery, at the same link please look at the options

Array: An array can be used for local data. There are two supported formats:

An array of strings: [ "Choice1", "Choice2" ]

An array of objects with label and value properties: [ { label: "Choice1", value: "value1" }, ... ]

It has been a long time since I worked with JSP but I think this link will help you encode stuff automatically.

Ajax jquery with JsonArray in JSP

Community
  • 1
  • 1
Steve0
  • 2,233
  • 2
  • 13
  • 22