File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Populating dropdown menu based on another Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Populating dropdown menu based on another" Watch "Populating dropdown menu based on another" New topic
Author

Populating dropdown menu based on another

S Ali
Ranch Hand

Joined: Aug 23, 2009
Posts: 129
Hi all,

I want to make a JSP form that has two drop down menus one for countries and another for cities.
Both menus are generated dynamically from the database,
and the city drop down menu should be populated once the user chooses a country from the other menu
as the content will be generated from the database based on his choice.
Is there any way that I can make this task using pure Java and not using other things like Ajax or Java Script
and of course without refreshing the page?

This is my code so far that generates the first drop down menu for Countries.


1- JSP form :







2- Countries bean:




Please help




SCJP 6
mahesh shinde
Ranch Hand

Joined: Jul 24, 2009
Posts: 69

why not Ajax.
if pure java then handle it using javascript
S Ali
Ranch Hand

Joined: Aug 23, 2009
Posts: 129
mahesh shinde wrote:why not Ajax.
if pure java then handle it using javascript


I'm a beginner and I don't know either of them
if you have an easy way to implement it using Ajax or JavaScript please tell me.
Karn Kumar
Ranch Hand

Joined: Aug 06, 2009
Posts: 146

Try Ajax you just need to search Ajax code which calls the Database from your jsp and without refresh,

or use simple onchange() and refresh the page with new data in another dropdown.
S Ali
Ranch Hand

Joined: Aug 23, 2009
Posts: 129
Chetan Dorle wrote:Try Ajax you just need to search Ajax code which calls the Database from your jsp and without refresh,

or use simple onchange() and refresh the page with new data in another dropdown.


How about getting the parameter of the first drop down list without having the user to click submit
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

There will have to be *some* JavaScript--if you don't want to refresh the page that's unavoidable.

You *could* just load all the data into the JSP if you don't want to use Ajax. But if you want to meet the requirements, time to learn something new so you can do your job.
S Ali
Ranch Hand

Joined: Aug 23, 2009
Posts: 129
Here is what I came up with

1- Jsp form:




2- City Bean:



3- Country Bean:




Now when a user makes a selection from the first drop down menu, it goes to the same JSP url with a parameter holding the selection and generates second drop down menu.

Big problem is second time the page is called the first drop down menu is back to the first selection
how do I get it to hold a user's selection?

As for the Javascript or Ajax soulotions I would love to learn something new but I keep googling and I can't understand because I'm a beginner at both topics.
I would love to get the job done professionally but I need someone to guide me through.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

First, start by not using scriptlets in your JSP--that's a nearly decade-old practice pretty much thoroughly discredited by now. JSTL and JSP EL are substantially cleaner and easier to maintain.

Restoring a previous selection is a simple matter of putting the selection(s) into scope (request, probably) and checking to see if the option you're currently writing matches the saved value.
S Ali
Ranch Hand

Joined: Aug 23, 2009
Posts: 129

<form method = "POST" action = "Regester.do">

<select name="mydropdown1" onChange="location.href='Regester.jsp?option='+this.value">
<%@ page import= "java.util.*" %>
<%@ page import= "country.*" %>
<%
Countries countryBean = new Countries();
for (String s: countryBean.getCountries())
{
if(request.getParameter("option") != null && request.getParameter("option").equals(s))
out.println("<option value="+s+" selected=\"selected\">"+s+"</option>");
else
out.println("<option value="+s+">"+s+"</option>");
}
%>
</select>

<select name="mydropdown2">
<%
City cityBean = new City(request.getParameter("option"));
for(String s : cityBean.getCities())
out.println("<option value="+s+">"+s+"</option>");
%>
</select>
<input type ="SUBMIT">

</form>


Thanks David, it worked.
I'll start reading about JSTL to make my code more professional.
S Ali
Ranch Hand

Joined: Aug 23, 2009
Posts: 129
Oh my god sorry guys but that solution is the worst ever
I have a big form to fill with lots of fields of course every time the page gets refreshed all data is lost.
Could anybody kindly guide me through the JavaScript or Ajax solution ?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

If you want example code for cascading dropdowns that uses jQuery to do this with Ajax, download the chapter 8 code for jQuery in Action at http://manning.com/bibeault2


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
divya chaudhary
Greenhorn

Joined: Jun 10, 2011
Posts: 4
hii...i am also stuck to this problem while making a project on java..cant we store the values in request or session object to avoid the loss of data in fields..
somebody please help..
thanks in advance..
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

You'll need to explain your issue more thoroughly.
divya chaudhary
Greenhorn

Joined: Jun 10, 2011
Posts: 4
solution given above :

<form method = "POST" action = "Regester.do">

<select name="mydropdown1" onChange="location.href='Regester.jsp?option='+this.value">
<%@ page import= "java.util.*" %>
<%@ page import= "country.*" %>
<%
Countries countryBean = new Countries();
for (String s: countryBean.getCountries())
{
if(request.getParameter("option") != null && request.getParameter("option").equals(s))
out.println("<option value="+s+" selected=\"selected\">"+s+"</option>");
else
out.println("<option value="+s+">"+s+"</option>");
}
%>
</select>

<select name="mydropdown2">
<%
City cityBean = new City(request.getParameter("option"));
for(String s : cityBean.getCities())
out.println("<option value="+s+">"+s+"</option>");
%>
</select>
<input type ="SUBMIT">

</form>

--------------------------------------------------------------------
problem :
when i select a value from mydropdown1 , i want mydropdown2 to get the values according to the one selected in mydropdown1..
in the above code due to the line..
...
...onChange="location.href='Regester.jsp?option='+this.value
...

however the value in mydropdown2 changes (comes according to the value in mydropdown1)but the code cannot retain the value selected in mydropdown1 resulting in the loss of data

divya chaudhary
Greenhorn

Joined: Jun 10, 2011
Posts: 4
so everytime the mydropdown2 gets the value according to mydropdown1..the value selected in mydropdown1 gets lost..and the default value appears in mydropdown1
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

If you are refreshing the page without restoring the values, that's what will happen. As you haven;t provided any details, hard to say.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Populating dropdown menu based on another