Solved

Passing JavaScript values to JSP variables

Posted on 2004-08-08
8
357 Views
Last Modified: 2008-03-04
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
0
Comment
Question by:The_Gutsy
8 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 11750496
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
0
 

Author Comment

by:The_Gutsy
ID: 11750594
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
0
 
LVL 35

Accepted Solution

by:
TimYates earned 125 total points
ID: 11750618
You will have to post a form...

Now, this form can be on another frame, so you *could* create a page with 2 frames, the main one, and an invisible one.  The invisible frame can have a form on it, where you set the value, submit the form, and it returns some javascript to fill in the value on the main frame...

You see what I mean?  Either that, or a popup "lookup" window, where a form opens in a new window, the user types in the ID, then posts the form, and the recieving jsp just uses javascript to write the data back into the main window...

That's the way it's going to have to be done :-/
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 35

Expert Comment

by:TimYates
ID: 11750693
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/
0
 
LVL 1

Expert Comment

by:quddhus
ID: 11773484
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>
0
 

Expert Comment

by:iglez
ID: 11897153
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
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

A procedure for exporting installed hotfix details of remote computers using powershell
For cloud, the “train has left the station” and in the Microsoft ERP & CRM world, that means the next generation of enterprise software from Microsoft is here: Dynamics 365 is Microsoft’s new integrated business solution that unifies CRM and ERP fun…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now