• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing variable value from javascript to jsp page at run time

 
Vijay Dongapure
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,...)
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vijay Dongapure
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic