This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

call java code from javascript

 
moshi cochem
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • 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.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • 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
  • 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
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • 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
  • 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
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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...
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have some misconceptions about how JSP works. You might find this article helpful.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • 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
  • 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 ?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic