/    Sign up×
Community /Pin to ProfileBookmark

from JavaScript to jsp or java classe

Hello,
Please, How can I call a Java method from JavaScript code?
or
How can I pass a JavaScript variableto a java code or jsp file?
I found the following code ,is the following code is the right solution?if the respose is no how should I do so.
thanks.

[CODE]<script src=”jquery-1.3.2.min.js” type=”text/javascript”></script>
<script src=”script.js” type=”text/javascript”></script>

<script type=”text/javascript”>
function file()
{
$(document).ready(function() {
$(‘#form’).submit(function() {
var number = $(‘#number’).val();

$.ajax({
type: “post”,
url: “calculate.jsp”,
data: “number=” +number,
success: function(msg) {

$(‘#result’).hide();

$(“#result”).html(“<h3>” + msg + “</h3>”)
.fadeIn(“slow”);
}
});

return false;
});
});

}
</script>
</head>
<body>
<form id=”form” action=”calculate.jsp” method=”post”>
Enter number:
<input id=”number” type=”text” name=”number” />

<input id=”submit” type=”submit” value=”Calculate Square Root” name=”submit” onclick=”file()”/>
</form>
<p id=”result”></p>
[/CODE]

[CODE]<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>JSP Page</title>
</head>
<body>
<%
int number = 0;
if(request.getParameter(“number”).matches(“4”))
{
number = Integer.parseInt(request.getParameter(“number”));
out.println(“Square root of ” + number + ” is ” + Math.sqrt(number));
}
else
{
out.println(“Enter a number!”);
}
%>
</body>
</html>[/CODE]

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@djowelleauthorNov 08.2011 — I am beginner with JavaScript field,and I need an exemple please.
Copy linkTweet thisAlerts:
@djowelleauthorNov 08.2011 — I am beginner with JavaScript field,and I need an example please.
Copy linkTweet thisAlerts:
@KorNov 08.2011 — is the following code is the right solution?[/QUOTE]
That is exactly the question which [I]you[/I] should give the answer: Is it? Does it work?

I would rather write this way:
<i>
</i>&lt;form id="form" action="calculate.jsp" method="post" [COLOR="Blue"]onsubmit="return file()"[/COLOR]&gt;
...
&lt;input type="submit" value="Calculate Square Root" /&gt;

To prevent the submitting, in order to let AJAX do its job. Do you know what AJAX is?

By the way, you have used a piece of code JQuery. JQuery is a structured JavaScript library and it has some custom syntax, different from the native JavaScript.
Copy linkTweet thisAlerts:
@djowelleauthorNov 09.2011 — please can you explain to me the part JavaScript of the previous code.

I want to know if this JavaScript code enable the use of a JavaScript variable in a java code?

thanks.
Copy linkTweet thisAlerts:
@KorNov 09.2011 — 
I want to know if this JavaScript code enable the use of a JavaScript variable in a java code?
[/QUOTE]

This is also what [I]I[/I] would like to know. ? How should [I]I [/I]know? It is your code, your server, your Java... I have no Java on my server... ?

Give it a test. In theory, it should work, no matter what server-side code is on the other end of the request: Java, PHP, ASP...


please can you explain to me the part JavaScript of the previous code.
[/quote]

I don't understand what do you want me to explain. What code? Whose code?
Copy linkTweet thisAlerts:
@djowelleauthorNov 09.2011 — I ask this because when I deleted the JavaScript part , the program work also.

It's not my code I found it in the Internet,and I'm training to understand it ?
Copy linkTweet thisAlerts:
@KorNov 09.2011 — I ask this because when I deleted the JavaScript part , the program work also.

It's not my code I found it in the Internet,and I'm training to understand it ?[/QUOTE]

Of course it works, because you encounter a JavaScript error (the function does not exist), thus the returned value is anything but a Boolean [B]false[/B], thus the submit action follows its genuine course.

Tu understand better, let me explain you what that [B]return[/B] does:

<i>
</i>&lt;form action="" onsubmit="return [COLOR="Blue"]false[/COLOR]"&gt;

That will prevent the submit action. Nothing happens.
<i>
</i>&lt;form action="" onsubmit="return [COLOR="Blue"]someFunction()[/COLOR]"&gt;

That will prevent the submit action only if the function someFunction() returns a Boolean [B]false[/B]. In any other case (the function returns anything else, the function does not return something special - which in fact means it returns itself as a value, which is equivalent with a Boolean true, or simply the function does not exist) the return value is equivalent with [B]true[/B], and the form is normally submitted.
Copy linkTweet thisAlerts:
@djowelleauthorNov 09.2011 — Thank you Kor for your help ?
×

Success!

Help @djowelle spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...