faqts : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

121 of 213 people (57%) answered Yes
Recently 4 of 10 people (40%) answered Yes

Entry

How to assign javascript variable into java/jsp variable?

Apr 5th, 2008 19:24
ha mo, Jean-Bernard Valentaten, Ruby Janto,


I don't think it is possible to pass JavaScript vars to a JSP 
Application else than to simply put them in a form with hidden inputs, 
since the JSP-Code is interpreted on the server, whereas JavaScript is 
client-based.
As for passing JS-vars to a Java Applet, there is a very easy way to do 
it.
First of all your applet-Tag must include "mayscript" (otherwise you 
can't access it through JS or any other client-based script-language).
Then the applet must have a name (ne.g. ame="myApplet") and the vars 
you'd like to acces have to be declared "public".
The rest is pretty easy. All you need to do is to assign the vars, like 
this
function JS2Java(someThing)
{
    if (document.all || document.getElementByID)
        document.myApplet.myVar = someThing;
    else if (document.layers)
        document.applets["myApplet"].myVar = someThing;
    else return;
}
You have to be carefull what kind of vars you want to pass!!! JS 
doesn't use Integer or String, a var is a var in JS. Java is strongly 
typed, so the only vars you can pass without making a big deal out of 
it, are int & String (float and char might work too, but I haven't 
tested yet).
If you want to pass String-type vars, you might have to change the code 
I've shown into this:
function JS2JavaString(someThing)
{
    if (document.all || document.getElementByID)
        document.myApplet.myVar = "" + someThing;
    else if (document.layers)
        document.applets["myApplet"].myVar = "" + someThing;
    else return;
}
The quotes ensure that you pass a String-type.
HTH
http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com