File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes Calling java method on a button click? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Calling java method on a button click?" Watch "Calling java method on a button click?" New topic
Author

Calling java method on a button click?

vishwanath nadimpally
Ranch Hand

Joined: Jan 25, 2005
Posts: 116
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Jan 25, 2005
Posts: 116
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

Joined: Jan 10, 2002
Posts: 60053
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
vishwanath nadimpally
Ranch Hand

Joined: Jan 25, 2005
Posts: 116
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

Joined: May 24, 2005
Posts: 28
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...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Calling java method on a button click?
 
Similar Threads
invoke java method when some event occur
a jsp beginner question
Calling Java Method
How call a servlet method
New to GUI, need help with actionListener and buttons