Solved

Passing JavaScript values to JSP variables

Posted on 2004-08-08
8
361 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem with Pie chart in Jsp 20 215
eclipse problems view not showing all the time 4 88
gZip compression filter 2 148
servlet concurrency 13 102
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Each year, investment in cloud platforms grows more than 20% (https://www.immun.io/hubfs/Immunio_2016/Content/Marketing/Cloud-Security-Report-2016.pdf?submissionGuid=a8d80a00-6fee-4b85-81db-a4e28f681762) as an increasing number of companies begin to…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

809 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