-1

I need to call java class method inside jquery , how do i do that ?

For more understanding Ex:

Class Foo{
      public static void letsCall(){
          // Do something here ...
      }
}

I want to call this letsCall() Method from jquery.

Baby
  • 5,062
  • 3
  • 30
  • 52
User123456
  • 179
  • 1
  • 5
  • 14

3 Answers3

2

1 approach:-

in jquery api, to call any backend system , in your case java, you need to do ajax call to invoke your java controller or service.

jquery ajax post call (call your java controller here)

2nd approach:-

if you need java class to take part in your jsp formation , you can go for custom tags, in your case you can look for jsf framework which basically bind your user events to your controller.

other frameworks - springmvc bind your jsp attributes to its corresponding models, in this case no need to use jquery , you can achieve it through spring custom tags.

spring mvc example

spring custom tags example

spring custom tag example

pappu_kutty
  • 2,378
  • 8
  • 49
  • 93
  • Thanks, I liked first approach , but it tends to use url as shown below,$.ajax({ type: "POST", url: url, data: data, success: success, dataType: dataType }); So , what do i pass in place of URL in my scenario .. As in i dont have a url to nevigate , just a java class with some methods... – User123456 Apr 02 '14 at 06:37
  • @ashish yeah your java class will reside in server, so you should provide your server location and controller action to which your request will be directed. see this link http://www.mkyong.com/spring-mvc/spring-mvc-jquery-autocomplete-example/ – pappu_kutty Apr 02 '14 at 06:40
  • if you want your jquery to call you java class, you need to host your java in a server and only through host url you can access your java class.. i dont know whether you can invoke plain pojo from jquery, it is not possible – pappu_kutty Apr 02 '14 at 06:57
  • I solved my Problem , i used Jetty handler to recieve requests , Process it & generate responses .. – User123456 Apr 03 '14 at 13:24
0

Calling a backend Java method from JavaScript/jQuery in JSP

Calling Java inside JavaScript Function

There are maybe a half dozen more similarly worded questions, please google first!

Community
  • 1
  • 1
Michael Voznesensky
  • 1,612
  • 12
  • 15
0

Check out DWR. We use it a lot to do this kind of thing and it works well. It wraps all the ajax stuff so you don't have to worry about it.

Taylor O'Connor
  • 137
  • 1
  • 4