Solved

Passing JavaScript values to JSP variables

Posted on 2004-08-08
8
367 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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!

 
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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
SimpleDateFormat.parse examples 19 123
How to Post an If Statement in JSP 3 70
glassfish admin console 1 53
JVM error from eclipse 1 27
Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
An introduction to the wonderful sport of Scam Baiting.  Learn how to help fight scammers by beating them at their own game. This great pass time helps the world, while providing an endless source of entertainment. Enjoy!
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

752 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