Skip to content Skip to sidebar Skip to footer

Ajax Autocomplete Textbox In Java Web Technology (jsp And Servlets)

Please I need your assistance on how to make HTML input text element work like 'Google's AJAX search engine' input text element with Java web technology (JSP, servlets and AJAX). D

Solution 1:

@user2870719 You can try like following, In your jsp page send a ajax request to a servlet/jsp and fill the response data in a javascript variable. So you can get the jQuery autocomplete textbox as i mentioned above.

<%@page import="java.sql.*"%>
<html><head><scriptsrc="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script><scripttype="text/javascript">functionshowData(value){ 
$.ajax({
    url : "ur_servlet?name="+value,
    type : "POST",
    async : false,
    success : function(data) {
//Do something with the data here
    }
});
}
</script></head><body><formname="employee"><inputtype="text"name="name"id="name"onkeyup="showData(this.value);"><br></table></body></html>

And in servlet,

String name = request.getParameter("name");
String buffer="";  
try{
   Class.forName("com.mysql.jdbc.Driver");
   Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root");
   Statement st=con.createStatement();
   ResultSet rs=st.executeQuery("select * from data where name like '"+name+"%'");
    while(rs.next())
    {
    buffer=buffer+"'"+rs.getString("name")+"',";
    }
response.getWriter().println(buffer);
}
 catch (Exception e) {
    e.printStackTrace();
 }

Finally send the response to jsp page. Let me know if this helps..

Solution 2:

Autocomplete- Jquery Ajax Json Example in Java(using Servlet)

Here am using devbridge jquery api to achieve the autocomplete functionality. Requirements: Eclipse Mysql (or you can use any other database ,but in lib folder you must add appropriate driver for that database) To get country list For Mysql https://github.com/bharathirasa/demo/blob/master/mysql-country-list-codes.sql

For Oracle https://github.com/bharathirasa/demo/blob/master/oracle-country-list.sql

HTML Code<input type="text" name="country" id="autocomplete" class="form-control" placeholder="Enter Country name" />

Jquery Code

     $("#autocomplete").autocomplete({
                              //lookup: countries,serviceUrl:'Auto', //tell the script where to send requestswidth: 450, //set width//callback just to show it's workingonSelect: function (suggestion) {
                              $('#selection').html('You selected: ' + suggestion.value + ', ' + suggestion.data);
                              },
                              showNoSuggestionNotice: true,
                              noSuggestionNotice: 'Sorry, no matching results',
               });

Servlet Code

                                     String q=request.getParameter("query");
                                     ArrayList<Country> o=CountryDao.getCountryName(q);
                                     Gsongson=newGson();
                                     // convert java object to JSON format,// and returned as JSON formatted stringStringjson= gson.toJson(o);
                                     //System.out.println(json);
                                     response.getWriter().write("{\"suggestions\":"+json+"}");

Here the method is not mentioned so all the data will be passed as GET method. If you want to pass the data through post mention in Jquery like type:’POST’. hi actually i created an autocomplete in java using devbridge jquery api

follow the link to get more info

http://pdfboxtutorial.blogspot.com/2015/03/autocomplete-jquery-ajax-json-example.html

Post a Comment for "Ajax Autocomplete Textbox In Java Web Technology (jsp And Servlets)"