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

该博客讨论了如何从Java类中调用一个方法,该方法返回一个JSON数组,并将其用于JavaScript页面上的数据网格。当数据库更新时,作者希望使用Ajax来刷新数据。他们已经创建了一个JSP页面来输出JSON字符串,但遇到问题在JavaScript中通过Ajax获取新的数据。解决方案建议使用jQuery的$.get方法来异步请求更新的数据。
摘要由CSDN通过智能技术生成

i have a java class ::

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 rowValues = new ArrayList();

List rowValues1 = new ArrayList();

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();

}

}

This is returning me a Json array. Now i want to call method() of this class in JavaScript again and again to get new values as i update my database. I have a data grid which is working fine as the page loads for the first time with a set of values in this json array. But how to refresh data using Ajax. Or how to call the method() using Ajax so that data gets refreshed when i click on a button on the page. The code where i am calling this method in java-script is ::

String temp1;

PopulateTextbox obj = new PopulateTextbox();

temp1 = obj.method();

%>

i am getting problem in retrieving new set of values in temp1 through a Ajax call to the server . please help ? Thanks.

解决方案

Create a blank JSP (for example, textBoxAjaxResp.jsp) and out put your JSON string from that JSP:

String temp1;

PopulateTextbox obj = new PopulateTextbox();

temp1 = obj.method();

%>

and hit that JSP using jQuery AJAX call.

$.get("mypath/textBoxAjaxResp.jsp", function (response) {

//do operation using the response

}, "json");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值