aspose file tools*
The moose likes JSP and the fly likes call java code from javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "call java code from javascript" Watch "call java code from javascript" New topic
Author

call java code from javascript

moshi cochem
Ranch Hand

Joined: Nov 10, 2009
Posts: 91
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.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41508
    
  53
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.


Ping & DNS - my free Android networking tools app
moshi cochem
Ranch Hand

Joined: Nov 10, 2009
Posts: 91
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
Marshal

Joined: Mar 22, 2005
Posts: 41508
    
  53
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

Joined: Nov 10, 2009
Posts: 91
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
Marshal

Joined: Mar 22, 2005
Posts: 41508
    
  53
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

Joined: Nov 10, 2009
Posts: 91
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

Joined: Nov 10, 2009
Posts: 91
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...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60994
    
  65

You have some misconceptions about how JSP works. You might find this article helpful.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41508
    
  53
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
Marshal

Joined: Mar 22, 2005
Posts: 41508
    
  53
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

Joined: Nov 10, 2009
Posts: 91
and basically I can put java code inside javascript function, that will be called
only if reaching the js function, right ?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60994
    
  65

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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: call java code from javascript