473,408 Members | 1,976 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 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 41686

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: StealthMonkey | last post by:
Let me prefix this by saying that I know next to nothing about Java (so please try to keep explainations simple). I use PHP for my server-side web programming. Here is my dilemma: I need a...
2
by: Eyal | last post by:
Hey, I would appriciate if anyone can help on this one: I have a java object/inteface having a method with a boolean parameter. As I'm trying to call this method from a javascript it fails on...
3
by: Rajesh | last post by:
Hi, I am using iplanet webserver 4.1. I want to call a java class from ssjs file. But I am not getting the result. I have created a java class file and put it in the folder...
18
by: Simula | last post by:
I am developing an HTML javascript application and I want to preserve state in a way that can be book-marked. I chose HTML anchors as a means of preserving state. When the application changes...
4
by: Anita C | last post by:
Hi, Can one call a c# function from within a javascript function ? I have a c# function GetLanguageItem(title), how can I call the above function from a javascript function in an .aspx page....
4
by: Martin Feuersteiner | last post by:
Dear Group I'm using VB to write an aspnet application. I would like to call a javascript function from within a VB Sub or VB Function, is it possible? My code is something like this: VB...
1
by: Lakshmi | last post by:
Hi All, I am having performance issues with the .NET client calling the Java Webservice running on axis. Have detailed the problem below. Please help. I wrote a webservice in Java. Lets name...
4
by: raghuvendra | last post by:
Hi I have a jsp page with 4 columns: namely Category name , Category order, Input field and a submit button. All these are aligned in a row. And Each Category Name has its corresponding Category...
2
by: humberto.bortolossi | last post by:
Greetings! I'm trying to control Java Applets using JavaScript. However, the standard recipes don't work under XHTML (they work well within HTML). See, for instance, the code ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.