View Full Version : Passing JavaScript Variable to JSP Page

11-16-2007, 02:45 PM

I am new to JavaScript.

I have read that there is a workaround to passing a JavaScript variable to a Java Server Page but would like confirmation that this is indeed possible and more information on exactly how to do this.

I have a JavaScript function that is called with an event when the user selects an item from a drop-down. I successfully capture this item in my JavaScript function and assign it to a javascript variable. Now, I would like to pass that variable back to my JSP (which is in the same file as the javascript) and ultimately use that variable as a condition to a query.

Anybody have ideas? Thanks.

11-16-2007, 04:36 PM
You need some sort of link or some way to use the variable as a query, a simple link could be:

<a id="passer" href="some.jsp">Whatever</a>

Then in your javascript, once you have assigned the value to the variable, let's say the variable is named lookup and the value (doesn't really matter too much) this time is 7. Then you can do:


That is a very basic way. Depending upon the rest of your code and markup, the basic concept (creating a URL with the query string appended to it, and giving the user some way of navigating to the URL) may be applied in many different ways.