Link to home
Start Free TrialLog in
Avatar of The_Gutsy
The_Gutsy

asked on

Passing JavaScript values to JSP variables

Can any body correct the follwing code

<Script language="JavaScript">
function test( x )
{
<%
int num = x;
num = num * 2;
%>

v.value = "<%out.print(num);%>";
}
<input type="button" name="b" value="test" onClick="test(5)">
<input type="text" name="v" value="0">

----------------------------------------------------------------------------------------------------

In short, I am trying to pass JavaScript value to JSP variable. I hope that it is possible to do that. If it is possible then how can I do it. I want to assing the variable x passed to the JavaScript function called test to the JSP variable called num.

Regards,
Ageel
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

you can't do that...  To pass a javascript variable (which is held on the client) to a JSP variable (which is held on the server), you will have to post it to the server as a form parameter.

That's because, it is only by form posting that client based javascript code can talk to server based JSP code...

<Script language="JavaScript">
function test( x )
{
    v.value = x * 2 ;
}
</script>
<input type="button" name="b" value="test" onClick="test(5)">
<input type="text" name="v" value="0">

You know you can do that I guess (and this was just an example) ;-)

You will have to build up whatever data you need into the HTML page dynamically using JSP, so that Javascript can use that data to do whatever processing you wanted to do on the server...

Tim
Avatar of The_Gutsy
The_Gutsy

ASKER

I have given you this simple code just as an example
but actually I need to pass the variables from javascript to jsp because I need to do so
Actually, I am using these parameters or let's say variables to get some information from the database. if java script can connect to the database that will be fine but I can't connect to the database using javascript without JSP

hmmmm
I get big headache, I don't knwo what I am supposed to do so that I solve this problem
ASKER CERTIFIED SOLUTION
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Actually, this looks like a good resource showing cool ways of doing what you want to do:

http://www-106.ibm.com/developerworks/web/library/wa-exrel/
you can assign java variable value to a javascript variable. since u need to get some information from db and use it in your javascript, first retrieve the data you want from db using java, then u can assign value to the javascript function.

For example:

<%
String num = DBUtil.getDBValue("loop");
%>

<html>
<head>
<Script language="JavaScript">
function test( x )
{
    var num1 = parseInt('<%=num%>');

    num1 = num1*2*x;

    v.value = num1;
}
</script>
</head>
<body>
    <form>
        <input type="button" name="b" value="test" onClick="test(5)">
        <input type="text" name="v" value="0">
    </form>
</body>
</html>
i agree still you can

pass from URL to the self page an then getParamter for example

jafa script

reload.jsp?Num='form.num1.value'

and get jsp

request.getParameter("Num");

jeje its mi idea