5

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


    }
}

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.

Shantanu Tomar
  • 1,572
  • 7
  • 38
  • 64
  • The code you've provided isn't Javascript, it looks like a JSP scriptlet (something else altogether). What exactly are you struggling with? Have you tried to write Javascript that makes an AJAX request? – Anthony Grist Mar 14 '12 at 10:30

3 Answers3

4

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

<%=temp1 %>

and hit that JSP using jQuery AJAX call.

$.get("mypath/textBoxAjaxResp.jsp", function (response) {
    //do operation using the response
}, "json");
Nemoy
  • 3,347
  • 1
  • 15
  • 14
  • Can u please explain in detail. creating a blank JSP means i have to write nothing inside it . and The code for my script let will remain on which page ? – Shantanu Tomar Mar 14 '12 at 10:44
  • yes a blank jsp and call your obj.method() inside that jsp and output the json string from that jsp set contentType to application/json – Nemoy Mar 14 '12 at 11:02
  • Hey i gt yr code. When i run my textBoxAjaxResp.jsp on a browser i am getting the output as a json array. But how to store that in a JavaScript variable so that i can pass it to source for my data grid? – Shantanu Tomar Mar 14 '12 at 11:22
  • modified my code, here response variable contain the parsed json response (js object). – Nemoy Mar 14 '12 at 11:41
  • Can u please help me out with this. http://stackoverflow.com/questions/9702098/loading-data-through-jquery-ajax-for-datagrid – Shantanu Tomar Mar 14 '12 at 12:37
1

you can use Direct Web Remoting library to call java functions using javascript

But I would suggest to write servlet (tutorial is pretty old but good, you should use servlet 3.0 if possible) yourself (instead of using DWR which will have their servlet to write to the response stream also this is very simple requirement so you should write servlet yourself instead of using third party library) and write JSON response directly.

Premraj
  • 7,802
  • 8
  • 45
  • 66
0

You might want to look at my answer here Updating contents of a jsp page without refreshing this uses a SpringMVC class which returns a text value from server side to client side using jquery ajax.

Community
  • 1
  • 1
David
  • 19,577
  • 28
  • 108
  • 128