• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Calling java method on a button click?

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67645
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
reply
    Bookmark Topic Watch Topic
  • New Topic