472,139 Members | 1,488 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,139 software developers and data experts.

Calling java within javascript



Hi,

I know I can call a static java method within javascript by using the
<% ... %tags. But how can pass a javascript variable ?

function thefunction()
{

var = javascriptvariable ;

<% testClass.set(var) ; %;

}
this works when i put a constant in

<% testClass.set("iiiiiiiiii") ; %;
Thanks!

Aug 30 '06 #1
9 41559

misdst23 wrote:
Hi,

I know I can call a static java method within javascript by using the
<% ... %tags. But how can pass a javascript variable ?
Since you are mentioning java, I'm assuming you are creating JSP's.
function thefunction()
{

var = javascriptvariable ;
'var' is a keyword in javascript. Proper usage will be like the
following:

var variable_name = value;
>
<% testClass.set(var) ; %;

}
To do what you want, you can do something like the following:

function myFunc()
{
<%
System.out.println("var variable_name = " + value);
%>
}

Your value will have to be surrounded by quotes depending on whether
it's a string value or not.

Aug 30 '06 #2

You're missing the point. I can't use a constant. I need to pass a
variable into Java.
web.dev wrote:
misdst23 wrote:
Hi,

I know I can call a static java method within javascript by using the
<% ... %tags. But how can pass a javascript variable ?

Since you are mentioning java, I'm assuming you are creating JSP's.
function thefunction()
{

var = javascriptvariable ;

'var' is a keyword in javascript. Proper usage will be like the
following:

var variable_name = value;

<% testClass.set(var) ; %;

}

To do what you want, you can do something like the following:

function myFunc()
{
<%
System.out.println("var variable_name = " + value);
%>
}

Your value will have to be surrounded by quotes depending on whether
it's a string value or not.
Aug 30 '06 #3

misdst23 wrote:
You're missing the point. I can't use a constant. I need to pass a
variable into Java.
I misunderstood your question. It seemed like you were asking how to
pass a java variable to javascript. For vice versa, it is not possible
through conventional methods.

Javascript is run on the client-side. Your servlet, i.e. compiled JSP
is run on server-side. The only solution I can think of right now is
for you to use XMLHttpRequest to send the data and use something like
request.getParameter() in your JSP.

Aug 31 '06 #4

I've come accross your point about javascript being on the client and
java on the server. BUT, the client side javascript function IS able to
pass a constant as a parameter to the server side java. It seems
logical to me that javascript should have a means to pass a variable.
Obviously, there is some sort of TCP connection going on in the back
end that allows the constant to be sent to the server. But since the
functionality is there maybe javascript should be standardized to allow
some way to pass a variable.
Thanks, I think i'll have to look into XMLHttpRequest.

Also, how can I use a image preload get to transmit a string to the
server, i've heard this is possible.

Thanks,

web.dev wrote:
misdst23 wrote:
You're missing the point. I can't use a constant. I need to pass a
variable into Java.

I misunderstood your question. It seemed like you were asking how to
pass a java variable to javascript. For vice versa, it is not possible
through conventional methods.

Javascript is run on the client-side. Your servlet, i.e. compiled JSP
is run on server-side. The only solution I can think of right now is
for you to use XMLHttpRequest to send the data and use something like
request.getParameter() in your JSP.
Aug 31 '06 #5
Hi,

misdst23 wrote:
I've come accross your point about javascript being on the client and
java on the server. BUT, the client side javascript function IS able to
pass a constant as a parameter to the server side java.
No, it's not. In your quoted example here under, the "constant" is a
Java string. The scope of everything which is executed between <% ... %>
is the server, and it's Java.

<% testClass.set("iiiiiiiiii") ; %>

"iiiiiiiiiii" is a Java string.

<snip>

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch
Aug 31 '06 #6

misdst23 wrote:
I've come accross your point about javascript being on the client and
java on the server. BUT, the client side javascript function IS able to
pass a constant as a parameter to the server side java. It seems
logical to me that javascript should have a means to pass a variable.
Obviously, there is some sort of TCP connection going on in the back
end that allows the constant to be sent to the server. But since the
functionality is there maybe javascript should be standardized to allow
some way to pass a variable.
Your missing something very key here, that is javascript cannot access
java.
What is can access are the RESULTS of that java method. You see by the
time javascript is running, the JSP has already rendered itself to HTML
output and is done.
>

Thanks, I think i'll have to look into XMLHttpRequest.
Using this you can pass a javascript variable to serverside process. If
this results in changes on the page, you'll have to either reload the
page or execute the changes yourself in javascript.
>
Also, how can I use a image preload get to transmit a string to the
server, i've heard this is possible.
By using a server side process (like a servlet) to provide the image
data, and appending a query string to the img src parameter. The server
side process will receive the request (headers, parameters and all) and
can process it before transmitting image data back (or it may not
actually send anything back...)

HTH.
>
Thanks,
Aug 31 '06 #7
"misdst23" <mi*********@yahoo.comwrote:
>
You're missing the point. I can't use a constant. I need to pass a
variable into Java.
If you're writing a *.jsp page, then the Java code will execute on the
server. The result will be HTML which will include your Javascript
functions. That will be sent to the client, and the Javascript will
execute there.

So, by the time the Javascript is running, the Java code has been run
and is no longer there.

--
Tim Slattery
Sl********@bls.gov
Aug 31 '06 #8

ok. So you're saying that the java within the javascript is actually
executed BEFORE the javascript executes ?
By using a server side process (like a servlet) to provide the image
data, and appending a query string to the img src parameter. The server
side process will receive the request (headers, parameters and all) and
can process it before transmitting image data back (or it may not
actually send anything back...)
So I have to have a seperate servlet or something that handles the
request on the server ? It can't be part of the same jsp.

Do you have any example code on this ?
Tom Cole wrote:
misdst23 wrote:
I've come accross your point about javascript being on the client and
java on the server. BUT, the client side javascript function IS able to
pass a constant as a parameter to the server side java. It seems
logical to me that javascript should have a means to pass a variable.
Obviously, there is some sort of TCP connection going on in the back
end that allows the constant to be sent to the server. But since the
functionality is there maybe javascript should be standardized to allow
some way to pass a variable.

Your missing something very key here, that is javascript cannot access
java.
What is can access are the RESULTS of that java method. You see by the
time javascript is running, the JSP has already rendered itself to HTML
output and is done.


Thanks, I think i'll have to look into XMLHttpRequest.

Using this you can pass a javascript variable to serverside process. If
this results in changes on the page, you'll have to either reload the
page or execute the changes yourself in javascript.

Also, how can I use a image preload get to transmit a string to the
server, i've heard this is possible.


HTH.

Thanks,
Aug 31 '06 #9

misdst23 wrote:
ok. So you're saying that the java within the javascript is actually
executed BEFORE the javascript executes ?
Yes. The javascript only sees the output resulting from the java
method.
>
By using a server side process (like a servlet) to provide the image
data, and appending a query string to the img src parameter. The server
side process will receive the request (headers, parameters and all) and
can process it before transmitting image data back (or it may not
actually send anything back...)

So I have to have a seperate servlet or something that handles the
request on the server ? It can't be part of the same jsp.
You will need something Java on the server side that can get your value
to your already executing javascript. I would recommend not using the
image loading idea, but rather use an XmHttpRequest to send a response
to and read the response from a servlet. Then use javascript to update
the page.
>
Do you have any example code on this ?
There are many examples/tutorials on using XmlHttpRequest.
>

Tom Cole wrote:
misdst23 wrote:
I've come accross your point about javascript being on the client and
java on the server. BUT, the client side javascript function IS able to
pass a constant as a parameter to the server side java. It seems
logical to me that javascript should have a means to pass a variable.
Obviously, there is some sort of TCP connection going on in the back
end that allows the constant to be sent to the server. But since the
functionality is there maybe javascript should be standardized to allow
some way to pass a variable.
Your missing something very key here, that is javascript cannot access
java.
What is can access are the RESULTS of that java method. You see by the
time javascript is running, the JSP has already rendered itself to HTML
output and is done.
>
>
Thanks, I think i'll have to look into XMLHttpRequest.
Using this you can pass a javascript variable to serverside process. If
this results in changes on the page, you'll have to either reload the
page or execute the changes yourself in javascript.
>
Also, how can I use a image preload get to transmit a string to the
server, i've heard this is possible.

HTH.
>
Thanks,
Aug 31 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by StealthMonkey | last post: by
4 posts views Thread by Anita C | last post: by
4 posts views Thread by Martin Feuersteiner | last post: by
4 posts views Thread by raghuvendra | last post: by
2 posts views Thread by humberto.bortolossi | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.