NewUser
NewUser

Reputation: 3819

Use ajax to retrieve data from Oracle database

I am completely new to AJAX. I have an HTML page where I need to fetch some data from the server. I followed a few tutorials from w3schools then I set foot on AJAX.

This is my strategy. I have one plain HTML file as ajax.html and a jsp file as ajaxdb.jsp. When a button is pressed I call a function updateList().

This is my code:

xmlhttp.open("POST", url, true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(passParameter);

    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
       document.getElementById("printBack").innerHTML = xmlhttp.responseText;
    }

I have made the visibility of printBack div hidden so whatever goes in there is invisible. In the jsp page I have a table which contains all the list of employees. After this I use dataTable.rows[0].cells[0].innerHTML = tempTable.rows[0].cells[0].innerHTML; Where tempTable is a reference to the hidden table.

I think this is not the optimal solution for retrieving table content in AJAX please suggest me how to solve it.

Upvotes: 0

Views: 2493

Answers (1)

am_
am_

Reputation: 2418

I´d recommend to use jQuery´s AJAX functionality to achieve this: http://api.jquery.com/jQuery.ajax/

Also from the JSP you should output the list of employees in a valid format (i.e.: JSON/XML). Then show this list on your ajax.html page by using the complete function which will trigger when the request finishes. (make sure you check that status sent was "success").

There are several libraries available to help you convert an java array/object into JSON/XML format.

EDIT You can for example use http://code.google.com/p/json-simple/ Then just put the data you need in your JSONobject, and output it. From example:

<%@page contentType="text/html; charset=UTF-8"%>
<%@page import="org.json.simple.JSONObject"%>
<%
JSONObject obj=new JSONObject();
obj.put("name","foo");
obj.put("num",new Integer(100));
obj.put("balance",new Double(1000.21));
obj.put("is_vip",new Boolean(true));
obj.put("nickname",null);
out.print(obj);
out.flush();
%>

Upvotes: 1

Related Questions