Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting Javascript variable into a JSP variable.

Status
Not open for further replies.

jsulman

Programmer
Jun 14, 2001
85
US
Greetings,
Does anybody know how to put variable from JavaScript to a Java variable in a JSP?
For example, lets say I have an html select box called sBox that I want to store it in a Java String object. Is there someway to store the currently selected option in sBox to String javaSBox.?

I know with scriptlets I can do this:
var holdValue = <%= javaValue %>

but I’m looking for something to the affect of:

doc = document.forms[0]
String javaSBox = doc.sBox.options[doc.sBox.selectedIndex].value;

Is there a way?

Thanks in Advance

Jeff Sulman
 
Keep in mind how a JSP works: the code is "traversed" from top to bottom and the HTML page is built and sent to the user's browser. At that point, the Java is effectively gone.

If you want to use form values or JavaScript values in the Java, then you need to submit those values as form elements to the JSP. Then, the JSP can retrieve them with the request.getParameter("myVar") statement.

Do you follow? Is this enough to go on?

Dave

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...east is east and west is west and if you take cranberries and stew them like applesauce
they taste much more like prunes than rhubarb does
[infinity]
 
Dave,
Yes, I follow. I thought that was the case but just wanted to be sure. Hoped there may be some majical way to do it.

Thanks for the quick response.

Jeff
 
Well, there might be some way to do what you REALLY want. You might just be asking the wrong question. What I mean is... why do you think you want to put the HTML-form-element-value into that Java String variable? What do you want to do with it? Does the form element have a default value when the page is drawn and is THAT what you're trying to get at? Do you want the page to be modified WITHOUT being redrawn when the user changes the value of the drop-down list? etc.

Dave

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...east is east and west is west and if you take cranberries and stew them like applesauce
they taste much more like prunes than rhubarb does
[infinity]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top