Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling java method on a button click?

 
vishwanath nadimpally
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way we can call a java method on clicking a html button using javascrip? Thanks for any help
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is called a posting back a form to the server. You can also look into Ajax which is a fancy way to postback to the server.

Eric
 
vishwanath nadimpally
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Eric. But I still can't fix my problem. This is the code I have inside my jsp.


I am building a url and using a 'href' to redirect to a servlet which calls a method.


I want to replace this above line with a


in such a way that I want to skip the call to the servlet and use the js function to determine the click and be able to call my java method directly.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64708
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot call a java method directly from Javascript. You might find this article an interesting read in order to find out why.

You can use Ajax to go back to the server and call your function from a servlet, or you could use the DWR Toolkit which uses Ajax to perform "direct web remoting" which is a pretty good approximation of an RPC call from JavaScript to server-side Java.
 
vishwanath nadimpally
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
You cannot call a java method directly from Javascript. You might find this article an interesting read in order to find out why.

You can use Ajax to go back to the server and call your function from a servlet, or you could use the DWR Toolkit which uses Ajax to perform "direct web remoting" which is a pretty good approximation of an RPC call from JavaScript to server-side Java.


Thanks Bear! Your article is very interesting. I have done a temporary fix, but in the long run I might end up using AJAX for this, because I am using this sort of calls in some places , plus I just bought the "AJAX in Action"
 
Orem Hun
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The elegant way is to use AJAX or some tools (GWT) based on AJAX. The other way is to set a hidden input variable, say command, a value in 'JS method' and re-submit. In your JSP, parse the command and call your Java method, and show the page. This is not as neat as AJAX as there is a submit and page refresh.

I'm curious to know what is your temporary fix...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic