Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Passing JavaScript values to JSP variables

Posted on 2004-08-08
8
Medium Priority
?
374 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 500 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post, I will showcase the steps for how to create groups in Office 365. Office 365 groups allow for ease of flexibility and collaboration between staff members.
There can be many situations demanding the conversion of Outlook OST files to PST format and as such, there is no shortage of automated tools to perform this conversion. However, what makes Stellar OST to PST converter stand above the rest? Let us e…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Screencast - Getting to Know the Pipeline

885 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