View Full Version : Assigning javascript variable to JSP variable using form submit or building a query

04-11-2005, 08:33 AM
Hi Geniuses,

I am developing a jsp page in which it takes input using javascript prompt
and i want to assign this value to Jsp variable I am not able to do it, Pls help me in this regard.

I have come across this problem in various forums and the standard answer given is use form submit or build a query. But no code along with it.

Since, this forum is different and people out here are very helpful I request you to send in the code snippet for assigning a value of javascript variable to a jsp variable by using submit form and if possible by building a query as well.

Thanks a billion,

04-12-2005, 08:48 AM
I am still waiting......

Have i posted this in the right forum?

04-12-2005, 09:52 AM
They are correct when they said you need to use form submission. You must be mistaking it that the server-side and client-side codes are executed as it is encountered in the page. That is, when a javascript prompt is invoked and the user types something, the returned value can be directly set to a jsp variable below it and then the server-side processing continues.

<script type="text/javascript">
var ans = prompt("...","");
String s = ans;

That will definitely not work. Take note that all server-side codes are processed first on the server then the server sends the generated html and javascript codes to the browser. The browser then renders the html and runs the javascript codes on the client-side.

So to do what you want, you need to store the return of the prompt dialog to a hidden field and then submit the form.
Or you can append the entered data in the URL querystring while redirecting to a jsp page.

var ans = prompt("Enter something:","");
location.href="process.jsp?ans=" + escape(a);

and then in process.jsp, retrieve the url parameter and do the necessary action on it.
The actual implementation depends on what you really want to accomplish.

04-13-2005, 06:41 AM
thank you Glenn...You really are a helpful member.

Similarly can you send me the code when form submission method is used...code snippets in jsp page..

Thanks once again,

04-13-2005, 07:04 AM
Please describe first in detail what you want to do.