Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / August 2006



Tip: Looking for answers? Try searching our database.

Calling java within javascript

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
misdst23 - 30 Aug 2006 23:19 GMT
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!
web.dev - 30 Aug 2006 23:34 GMT
> 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.
misdst23 - 31 Aug 2006 00:32 GMT
You're missing the point. I can't use a constant. I need to pass a
variable into Java.

> > Hi,
> >
[quoted text clipped - 28 lines]
> Your value will have to be surrounded by quotes depending on whether
> it's a string value or not.
web.dev - 31 Aug 2006 01:16 GMT
> 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.
misdst23 - 31 Aug 2006 01:29 GMT
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,

> > You're missing the point. I can't use a constant. I need to pass a
> > variable into Java.
[quoted text clipped - 7 lines]
> for you to use XMLHttpRequest to send the data and use something like
> request.getParameter() in your JSP.
Laurent Bugnion - 31 Aug 2006 07:21 GMT
Hi,

> 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
Signature

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

Tom Cole - 31 Aug 2006 12:20 GMT
> 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
[quoted text clipped - 4 lines]
> 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,
misdst23 - 31 Aug 2006 14:31 GMT
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 ?

> > 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
[quoted text clipped - 23 lines]
>
> > Thanks,
Tom Cole - 31 Aug 2006 15:59 GMT
> 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
[quoted text clipped - 4 lines]
> 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.

> > > 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
[quoted text clipped - 23 lines]
> >
> > > Thanks,
Tim Slattery - 31 Aug 2006 13:54 GMT
>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
Slattery_T@bls.gov
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.