• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Populating dropdown menu based on another

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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



 
Ranch Hand
Posts: 69
Eclipse IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why not Ajax.
if pure java then handle it using javascript
 
S Ali
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 153
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


<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
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll need to explain your issue more thoroughly.
 
divya chaudhary
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic