• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

call java code from javascript

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a jsp page that contains a few html controls.
When clicking on the button, I want to take the values from
those controls and to send them to a jar file.
I have the code of calling the jar - it is java - code, but
I don't know how to call that java code from javascript - when clicking on the button.
Thanks.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the jar file is part of a web app, then you can use AJAX (specifically, JavaScript's XMLHttpRequest object) to make a call to a server-side URL. Various libraries like Prototype make this quite easy.
 
moshi cochem
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, but I have the reference to the jar, I have the code to call the appropriate function in it.
The only problem is to invoke this code when there is a javascript button click.
Thanks.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by "the reference to the jar"? Do you want to execute the Java code on the client or on the server?
 
moshi cochem
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I understand.
So , let me ask this:
Can I invoke that java code from the server side ?
Can I make a postback thru the html button click event and to catch it when the forms posts, and to invoke there the java code ?
Something without ajax ?
thanks.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can I invoke that java code from the server side ?


Assuming that the server-side consists of a Java web app, then it can execute any code you want.

Can I make a postback thru the html button click event and to catch it when the forms posts, and to invoke there the java code ? Something without ajax ?


You can handle the button click in JavaScript, and have the script code call the server through XHR (or a library that uses it). In response, the server-side can execute any Java code you desire, and send some result back to the client. That is generally called AJAX, even though no actual XML need be involved. There is no other way without doing a full form submit to the server.
 
moshi cochem
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your answer.
In any case, I want to add some code here to be explained well.

<script>
function UpdateData()
{
var dataLimit = document.getElementById("txtDateLimit").value;
var countLimit = document.getElementById("txtCountLimit").value;

<%
String str = HaspDemo.CheckFlow(dataLimit, countLimit); //the javascript values...
%>
}
</script>
<body>
<input type="button" onclick="UpdateData()" value="Save">

What , in your opinion is the best way to achieve ? same solution you gave me ?
Thanks.
 
moshi cochem
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you think about this solution:
Actually, in the javascript function I can call my same page and to send in the url the values.
In the page body, I right java code that anyways works, and check if there are params in the request.
I so, it updates them...
 
Sheriff
Posts: 67747
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 have some misconceptions about how JSP works. You might find this article helpful.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you intend won't work. We covered that here, didn't we? So you should implement an AJAX solution just like you did for that problem.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

in the javascript function I can call my same page and to send in the url the values.
In the page body, I right java code that anyways works, and check if there are params in the request.
I so, it updates them...


That would work, although it's considered bad style to keep any Java code in JSPs. You should use a servlet that executes the logic, and which then dispatches to the JSP.
 
moshi cochem
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and basically I can put java code inside javascript function, that will be called
only if reaching the js function, right ?
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can write a JavaScript function that makes a request to the server and have code on the server (written in Java) do stuff with any values passed with the request.
 
Bear Bibeault
Sheriff
Posts: 67747
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

moshi cochem wrote:and basically I can put java code inside javascript function, that will be called
only if reaching the js function, right ?


Not right. Did you read the article I linked to?
 
Time is the best teacher, but unfortunately, it kills all of its students - Robin Williams. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic