File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Using Java Objects in AJAX?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Using Java Objects in AJAX??" Watch "Using Java Objects in AJAX??" New topic

Using Java Objects in AJAX??

Shridhar. Blr.
Ranch Hand

Joined: Nov 23, 2000
Posts: 45
Hi All,

I am new to Ajax, can you explain..

1. How can i use Java Objects in Ajax..
2. On Some Action/Event, i want to populate the data in Objects, send it back to the client & use the same in UI.. How can i do this??..
3. I have 4 blocks/tables in the UI, On some action's response, i need to populate only How can i populate the data only the required block/table, how can i notify, which table needs to be updated..

Also, please let me know if there are any articles on the same with examples..

Any help would be highly appreciated..
Surya Kant
Ranch Hand

Joined: Mar 29, 2005
Posts: 104
I m not clear about what you are really expecting.But stil i wil try.
Ajax is a technology in which u can send request to server & get the response without refreshing page.
In ur case u can set response in ur action file and get that response in jsp.
with that u can refersh whtever u want.
I know u are not cleared with this description.but still if u find any difficulties in ajax feel free to contact me..
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I've found that the best way to get a handle on AJAX is to download some open source code and try a tutorial. I've been working with DWR for some time now. This is an open source project that integrates AJAX with java. I've found it to be surprisingly easy to use and integrate in to a Struts project or any other J2EE web project for that matter.

Here's a link for a good tutorial:

AJAX in a nutshell is a process that allows you use javaScript to invoke a server-side process, return the results to your javaScript code, and then use javaScript to modify the document without totally redisplaying it. In javaScript, you modify a document by using the Document Object Model (DOM). This is a set of APIs that allow you to replace elements of the document, delete elements, and add new elements.

There's an old saying. "He who is good with a hammer, tends to think that everything is a nail". Once you get good with AJAX, don't make the mistake of thinking it should be used for everything. For most tasks in a Web application, the good old fashioned method of sending a request to the server and redisplaying the page really is the best way to do it.

There are certain problems, though, that AJAX solves beautifully. Here's an example. Suppose your website takes names and addresses of people all over the world. You want the Countries to be in a drop-down box, as well as the states/provinces. There's a problem, though. You don't know which states/provinces to choose from until the user has selected a country. There have been different ways to solve this problem using conventional request-response html, but none of them were very elegant.

With AJAX, it becomes a breeze. Use the onchange event of the country drop-down box to trigger an AJAX call to the server. Upon return, it calls some javaScript that populates the state/province drop-down with entries appropriate to the country selected.

I hope this points you in the right direction. Once you've tried a tutorial or two, let us know if you still have questions.

Consultant, Sima Solutions
Shridhar. Blr.
Ranch Hand

Joined: Nov 23, 2000
Posts: 45

Thanks for your quick reply..

What i understand is "The response can have any text-based format but, as its name suggests, XMLHttpRequest has built-in capabilities for dealing with XML response data. This makes XML the default choice for Ajax responses.."
So i want to know -- If i send back the data from the Server to the client as Objects {like in Servlet/JSP we pass the Objects in session or request Objects & get the values from session or request, loop these objects to render the data} can i use the XMLHttpRequest to loop & fetch data.. If this is not possible, Is there a technique to achive the same..

Hope, this time i have made it clear..

Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
AJAX is a way of communicating from a javaScript client to a server. Just as a browser normally communicates to a server via HTML, AJAX communicates via XML. There is no way that a javaScript program running on a client browser can directly access a java object that resides on a server. The client sends an XML request to the server and gets back an XML response.

The beauty of a framework like DWR is that it makes it seem like you're communicating directly with java objects. It handles all the XML marshalling and unmarshalling for you. You just call a javaScript function that has the same parameters as your java object's method, and DWR does the rest. If your java method is sending back a javaBean (An Address bean, for example) DWR will create a javaScript object that closely resembles your javaBean, and you can access it's properties the same as you would a javaBean.
subject: Using Java Objects in AJAX??
Similar Threads
Using JTable
AJAX and table
AJAX required or not
Swing client Vs Applet Vs RIA (AJAX, etc)
Ajax drop down