Java

Sunday 25 November 2012

Jquery autocomplete from database restful webservice json,ajax:


Jquery autocomplete from database restful webservice json,ajax:
                                                              


TUTIORALS FROM MADHAV:
 

     JAVA-SERVLETS     JAVA-JDBC     JAVA-JSP       HIBERNATE-SHCEMABASED 

    SPRING-AOP-ANNOTATIONS      SPRING -DAO     SPRIN-MVC     SPRING-SECUTITY

     SPRING-DATA-JPA     REST-WEB-SERVICE     STRUTS2HIBERNATE    GWT.... 
                                                           
                                                                        
                                                             




Data base values:
Select * from department;

auto.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>sa
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'index.jsp' starting page</title>
       <meta http-equiv="pragma" content="no-cache">
       <meta http-equiv="cache-control" content="no-cache">
       <meta http-equiv="expires" content="0">   
       <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
       <meta http-equiv="description" content="This is my page">
       <!--
       <link rel="stylesheet" type="text/css" href="styles.css">
       -->
       <script type="text/javascript" src="js/jquery.min.js"></script>
       <link href="css/madh/jquery-ui-1.9.1.custom.css" rel="stylesheet">
      
       <script src="js/jquery-1.8.2.js"></script>
       <script src="js/jquery-ui-1.9.1.custom.js"></script>
  </head>
 
  <body bgcolor="wheat">
   

<script type="text/javascript">

  $(document).ready(function() {
 $("#presidentsClientInput").autocomplete({
                source: function( request, response ) {
                $.ajax({
                     type: "GET",
                    url: "services/dept",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(data) {
                           
                              response(data);
                        }
                    });
                }
             
            });
             });



</script>

<label for="presidentsClientInput">Search: </label>
    <input id="presidentsClientInput" />
   
  </body>
</html>      


Dept.java

package madhav.pojodao;


public class Dept {
      public int deptno;
      public String dname,loc;
      public int getDeptno() {
            return deptno;
      }
      public void setDeptno(int deptno) {
            this.deptno = deptno;
      }
      public String getDname() {
            return dname;
      }
      public void setDname(String dname) {
            this.dname = dname;
      }
      public String getLoc() {
            return loc;
      }
      public void setLoc(String loc) {
            this.loc = loc;
      }
}


package madhav.daoimpl;
import madhav.pojodao.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.*;
import java.util.*;
public class DeptDao {
            JdbcTemplate jt;
            public DeptDao(JdbcTemplate jt)
            {
            this.jt=jt;
            }
            public List<Dept> selectRecords()
            {
                 String sql="select d.deptno as deptno, d.dname as dname, d.loc as loc from department d"; 
                 System.out.println("select record form Rommapper");
                  DeptRecord dd=new DeptRecord();
                 List<Dept> depts=jt.query(sql, dd);
                 System.out.println(depts.size()+"-------------");
                 return depts;
                 }
            private static final class DeptRecord implements RowMapper<Dept>
            {
                        public Dept mapRow(ResultSet rs, int rownum) throws SQLException {
                                    // TODO Auto-generated method stub
                                    Dept d=new Dept();
                                    d.deptno=rs.getInt("deptno");
                                    d.dname=rs.getString("dname");
                                    d.loc=rs.getString("loc");
                                    return d;
                        }
            }




DeptResource.java



package madhav.api;                                                                  
import java.util.ArrayList;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.Path;

import madhav.daoimpl.*;
import madhav.pojodao.Dept;

import org.springframework.beans.factory.*;
import org.springframework.beans.factory.xml.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.*;
import org.apache.commons.dbcp.BasicDataSource;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;

@Path("/dept")
public class DeptResource {
    @GET
    @Produces("application/json")
    public String getClichedMessage()  {
       //spring dao
      
        JSONObject jsonObj = null;
        JSONArray jsArray =null;
        String names=null;
       try
       {
              jsonObj=new JSONObject();
              jsArray = new JSONArray();
               DeptDao deptDao;
             ApplicationContext beanfactory;
             beanfactory=new ClassPathXmlApplicationContext("applicationContext.xml");
             deptDao = (DeptDao)beanfactory.getBean("deptdao");
             System.out.println("extract data");
            List<Dept> depts= deptDao.selectRecords();
           Iterator<Dept> it=depts.iterator();
            while(it.hasNext())
            {
                    Dept d=it.next();
                    jsArray.put(d.dname);
       }
      
             }
       catch(Exception e)
       {     
       }
              return jsArray.toString();
    }
}


ApplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
                xmlns="http://www.springframework.org/schema/beans"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:p="http://www.springframework.org/schema/p"
                xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/mysql</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>mysql</value>
</property>
</bean>
<bean id="jdbctemp" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg>
<ref local="datasource"/>
</constructor-arg>
</bean>
<bean id="deptdao" class="madhav.daoimpl.DeptDao">
<constructor-arg>
<ref local="jdbctemp"/>
</constructor-arg>
</bean>

</beans>



Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
       xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
       http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>  
  <servlet>
       <display-name>JAX-RS REST Servlet</display-name>
       <servlet-name>JAX-RS REST Servlet</servlet-name>
       <servlet-class>
              com.sun.jersey.spi.container.servlet.ServletContainer
       </servlet-class>
       <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
       <servlet-name>JAX-RS REST Servlet</servlet-name>
       <url-pattern>/services/*</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>




Accessing rest services by using fireforx  rest client:










Accessing rest services by using  client as html: