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.