wood burning stoves 2.0*
The moose likes Struts and the fly likes Passing variable value from javascript to jsp page at run time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Passing variable value from javascript to jsp page at run time" Watch "Passing variable value from javascript to jsp page at run time" New topic
Author

Passing variable value from javascript to jsp page at run time

Vijay Dongapure
Greenhorn

Joined: Sep 15, 2006
Posts: 2
How can we pass the variable value from javascript to jsp page at runtime? I am getting this problem when i want to display the second drop down box according to the selected item in the first drop down.
For ex: Lets consider we have a drop down field named Gender(Male,Female).
If we select Male then the next Drop Down has to be shown as (Kumar, Mr,...)


Thanks & Regards<br />Vijay S. Dongapure<br /> <br />Programming was never easier before
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
It's important to realize that JavaScript variables live on the client-side (Browser) and that JSP variables live on the server-side, so the two can't really talk to each other at run time. However, now that AJAX has become popular, it is possible to have JavaScript submit a request to the server and receive a response without refreshing the page.

Here are two possible ways of handling a dependent select box.

1- Use the onchange event of the first select box to submit the form. Then have the action class read the value of the first select box, populate the values for the second box, and redisplay the page. In order for this to work, you will have to set some sort of flag to indicate you're only submiting the form to change the select box, not to signify the data is complete and ready to be processed.

2- In the original JSP, display the second select with no options. Use the onchange event of the first select box to make an AJAX call to the server, which retrieves the set of options for the second select box. You would then use the Document Object Model (DOM) API of JavaScript to add the options to the second select box. If you're not familiar with AJAX, I'd suggest using a framework such as DWR to handle your AJAX calls. Otherwise, you're going to have to spend some time learning the details of how to make AJAX calls.

If you just want to get your project done on time, I'd suggest using method 1. If you want to explore new territory, method 2 would be a good choice.


Merrill
Consultant, Sima Solutions
Vijay Dongapure
Greenhorn

Joined: Sep 15, 2006
Posts: 2
Thanks for the reply. As i am not having that much amount of time to learn AJAX now i will implement the first method. Thanks again
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing variable value from javascript to jsp page at run time
 
Similar Threads
DropDown List Problem : file system
dependent drop down list
Resfresh page and getting values when option is selected
move date 30 days
disabled field with selected value