2012-03-14 71 views
5

我有一個Java類::如何在javascript中使用ajax調用java類方法?

package MyPackage; 

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 
import java.util.ArrayList; 
import java.util.List; 

import net.sf.json.JSONArray; 
import net.sf.json.JSONObject; 

import com.google.gson.Gson; 

public class PopulateTextbox { 

    Gson gson = new Gson(); 
    JSONObject obj = new JSONObject(); 
    JSONArray arr = new JSONArray(); 
    String temp1; 

    String temp; 
    List <String>rowValues = new ArrayList<String>(); 
    List <Integer>rowValues1 = new ArrayList<Integer>(); 
    String[] contactListNames; 
    Connection con=null; 
    Statement st=null; 
    ResultSet rs=null; 


    public String method(){ 


     try{ 


     String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; 
     Class.forName(driver); 

     String db = "jdbc:odbc:Practice_Database"; 
     con = DriverManager.getConnection(db,"",""); 

     st = con.createStatement(); 
     String sql = "SELECT Emp_Name,ID,Email_Add FROM EmployeeSearch"; 
     rs = st.executeQuery(sql); 

     while(rs.next()){ 
      obj.put("ID", rs.getInt("ID")); 
      obj.put("Names",rs.getString("Emp_Name")); 
      obj.put("Email", rs.getString("Email_Add")); 
      arr.add(obj); 

     } 
     //obj.accumulate("ID",rowValues1); 

     //obj.accumulate("Names",rowValues); 
     temp1 = arr.toString(); 
     System.out.println(temp1); 

    }catch(Exception e){System.out.println(e);} 
    /*finally{ 
     try { 
       if(con!=null)con.close(); 
      } catch (SQLException e) { 

       e.printStackTrace(); 
      } 
     try { 
      if(rs!=null)rs.close(); 
     } catch (SQLException e) { 

      e.printStackTrace(); 
     }try { 
      if(st!=null)st.close(); 

     } catch (SQLException e) { 

      e.printStackTrace(); 
     } 


    }*/ 
     return temp1; 

    } 
    public static void main(String args[]) 
    { 
     PopulateTextbox obj = new PopulateTextbox(); 
     String temp1= obj.method(); 


    } 
} 

這是我返回JSON數組。現在我想在JavaScript中一次又一次地調用這個類的method()來獲取新的值,當我更新我的數據庫時。我有一個數據網格工作正常,因爲頁面首次加載這個json數組中的一組值。但是如何使用Ajax刷新數據。或者如何使用Ajax調用方法(),以便在單擊頁面上的按鈕時刷新數據。我在哪裏調用Java的腳本這個方法的代碼::

<% 

    String temp1; 
    PopulateTextbox obj = new PopulateTextbox(); 
    temp1 = obj.method(); 
    %> 

我是通過Ajax調用服務器中檢索新的temp1設定值的獲得問題。請幫忙 ?謝謝。

+0

您提供的代碼不是Javascript,它看起來像一個JSP scriptlet(完全不同)。你究竟在爲什麼而掙扎?你有沒有試過編寫使AJAX請求的JavaScript? – 2012-03-14 10:30:26

回答

4

創建一個空白JSP(例如,textBoxAjaxResp.jsp)進出把你的JSON字符串從JSP:

<% 

String temp1; 
PopulateTextbox obj = new PopulateTextbox(); 
temp1 = obj.method(); 
%> 

<%=temp1 %> 

,打的是JSP使用jQuery AJAX調用。

$.get("mypath/textBoxAjaxResp.jsp", function (response) { 
    //do operation using the response 
}, "json"); 
+0

請你詳細解釋一下。創建一個空白的JSP意味着我不得不在裏面寫任何東西。和我的腳本讓代碼將保持在哪個頁面上? – 2012-03-14 10:44:19

+0

是一個空白的jsp,並在該jsp中調用obj.method(),並從該jsp輸出json字符串 將contentType設置爲應用程序/ json – Nemoy 2012-03-14 11:02:52

+0

嘿,我是gtr代碼。當我在瀏覽器上運行我的textBoxAjaxResp.jsp時,我得到的輸出爲json數組。但是,如何將它存儲在JavaScript變量中,以便我可以將它傳遞給我的數據網格源? – 2012-03-14 11:22:11

1

可以使用Direct Web Remoting庫使用JavaScript

調用Java的功能,但是我建議寫servlet(教程是很老,但良好的,你應該使用servlet 3.0如果可能的話)你自己(而不是使用DWR這將讓他們的servlet寫入響應流也是非常簡單的要求,所以您應該自己編寫servlet而不是使用第三方庫)並直接編寫JSON響應。