0

Using jQuery AJAX, can we call a specific JAVA method (e.g. From an Action class)

The returned data from that Java method would be used to fill in some HTML code.

Please let me know if this can be done easily using jQuery (like it does in DWR)..Also for multiple data points in HTML, do we need to make multple AJAX requests?

3
  • 1
    My guess is that the downvoters don't know anything about DWR, therefore think that "calling Java from JavaScript isn't possible". (Which is true (ignoring applets), but DWR makes it look like you are.) Downvoters that don't include a reason help no one. Commented Sep 25, 2011 at 14:10
  • I imagine its downvoted because its an exact duplicate of this stackoverflow.com/q/7544362/106261 and a rather unspecific question Commented Sep 25, 2011 at 14:13
  • @NimChimpsky Hmm, yeah; same user. Fair enough. Although I don't think it's an exact duplicate; this one is following up with a technology-specific approach. It's vague, but initial questions when figuring something out often are. Commented Sep 25, 2011 at 14:15

2 Answers 2

1

The simple answer is you map your ajax calls to urls, which are in turned map to methods in your java code. The Ajax -> URI mapping happens on the client side (which ever js framework you are using, and the URI -> specific handler mapping happens within the java application)

What java framework are you using? There should be very clear and simple documentation on how to do this. For standard Java EE mappings (meaning you are not using any frameworks like Spring or Roo) I found this on google: http://javapapers.com/servlet/what-is-servlet-mapping/

"For multiple data points in HTML" I assume you are talking about having multiple parts of the html update. You can do this with multiple requests, or you can do it with one request. If you do the latter, the server needs to return all the data you need to update the dom appropriately.

Sign up to request clarification or add additional context in comments.

3 Comments

I am using Struts in my application and could u please give an example or explain how 1 reuest can be used to update multiple parts of html?
@testndtv The nutshell version is that you'd get JSON back, and use it to change the DOM. For example, the JSON might include a "success" message and the results of a query. The Ajax callback could take the message and put it in one DOM area, and put the query results in another.
So you are basically saying to have the JAVA method return a JSON string and then use them to fill multiple places in html ?
0

It's not as transparent as with DWR--DWR handles making JavaScript look like Java. With jQuery you'll get JSON (or just HTML if/when it's easier that way). It's still pretty straight-forward, though. You'd send the Ajax request to a URL, rather than having it look like a local method call.

I'm not sure what you mean by "multiple data points in HTML" -- you get back whatever data you get back, and you can do with it whatever you want. If the response has all the data you need, then you wouldn't need to make multiple requests.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.