如何使用ajax在javascript中调用java类方法?

时间:2012-03-14 10:25:16

标签: java javascript ajax datagrid

我有一个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调用method(),以便在单击页面上的按钮时刷新数据。我在java脚本中调用此方法的代码是::

<%

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

我在通过对服务器的Ajax调用中检索temp1中的新值集时遇到问题。请帮忙 ?谢谢。

3 个答案:

答案 0 :(得分:4)

创建一个空白JSP(例如,textBoxAjaxResp.jsp)并从该JSP中输出您的JSON字符串:

<%

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

<%=temp1 %>

使用jQuery AJAX调用点击该JSP。

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

答案 1 :(得分:1)

您可以使用Direct Web Remoting库使用javascript

调用java函数

但我建议你自己编写servlet(教程很老但很好,你应该使用servlet 3.0)(而不是使用DWR,它们的servlet会写入响应流)这也是非常简单的要求所以你应该自己编写servlet而不是使用第三方库)并直接编写JSON响应。

答案 2 :(得分:0)

您可能希望在此处查看我的答案Updating contents of a jsp page without refreshing这使用SpringMVC类,该类使用jquery ajax从服务器端向客户端返回文本值。

相关问题