jsp list box

jsp list box

I have two list boxs. the values for the first list box is retrieved from the mysql database. I want to fill the second list box selected item from the database.

Please help me in this regard.

Your help is highly appreciated.
View Answers

April 28, 2010 at 4:48 PM

Hi Friend,

Try the following code:

<%@ page language="java" %>
<%@ page import="java.sql.*" %>
<script>
function city(){
var cid=document.getElementById("country").selectedIndex;
var val = document.getElementById("country").options[cid].text
window.location.replace("http://localhost:8080/examples/jsp/country.jsp?id="+cid+"&&value="+val);
}
</script>
<%!
Connection conn = null;
ResultSet rs =null;
Statement st=null;
String query="";
int cid;
%>
<%
String value=request.getParameter("value");

Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root";, "root");
st = conn.createStatement();
rs = st.executeQuery("select * from country");
%>
<select id="country" onchange="city();">
<option value="0">--Please Select--</option>
<% while(rs.next()){ %>
<option value="<%=rs.getString("countryname")%>"><%=rs.getString("countryname")%></option>

<% if(rs.getString("countryname").equals(value)){%>
<option value="<%=value%>" selected disabled><%=value%></option>
<%
}
}
%>
</select>
<select>
<%
String id=request.getParameter("id");
rs=st.executeQuery("select * from city where countryid='"+id+"'");
while(rs.next()){
%>
<option value="<%=rs.getString("city")%>" ><%=rs.getString("city")%></option>
<%
}
%>
</select>
</body>
</html>

For the above code, we have created two database tables:
1)'country'
CREATE TABLE `country` (
`countryid` bigint(30) NOT NULL auto_increment,
`countryname` varchar(40) default NULL,
PRIMARY KEY (`countryid`)
)

2)'city'
CREATE TABLE `city` (
`id` bigint(40) NOT NULL auto_increment,
`countryid` int(20) default NULL,
`city` varchar(40) default NULL,
PRIMARY KEY (`id`)
)

Thanks









Related Tutorials/Questions & Answers:
List Box - JSP-Servlet
list box.on selecting one value in list box,i will retrieve values from database and store into another list box in same page using javascript :location.my problem is in first list box,value which i selected to get values is not displaying
jsp list box - Java Beginners
jsp list box  I have two list boxs. the values for the first list box is retrieved from the mysql database. I want to fill the second list box...("http://localhost:8080/examples/jsp/country.jsp?id="+cid+"&&value="+val
Advertisements
Javascript List Box - JSP-Servlet
itself.my problem is in list box the semester which i selected is not showing in list box as selected.when i select,the page refreshes but i get the result what i expected.i need to show in list box as semester is selected but it doesnt
scroll bars to list box - JSP-Servlet
scroll bars to list box  Can I add scroll bars to a list box in struts?  Hi friend, Scroll the list box in struts Two attribute set "multiple" "size". Select Tag Example Select Tag
retrive the data from access database to drop down list box in jsp
retrive the data from access database to drop down list box in jsp  hai, im new to jsp now im using the jsp along with access database.in table i load all the data's i need to retrive the data from database to dropdown list box
Dynamic loading of Combo box list using servlet - JSP-Servlet
Dynamic loading of Combo box list using servlet  I have the category... I give theortical descrption read it! Select a value from drop dwon box... and the resulst set which you getting passed into your jsp page. Then iterate
combo box - JSP-Servlet
combo box  how to get a combo box in jsp page which allows editing as well as list box
add text box and select list dynamically and get its value to store it into database using jsp request parameter
add text box and select list dynamically and get its value to store it into database using jsp request parameter  its very helpful when you have only... added select box also into next jsp page plz reply me as early as posssible
input type dialog box take text and search a list and focus that text - JSP-Servlet
input type dialog box take text and search a list and focus that text  i have a button..when i press it will prompt a dialog box with a text box... then it will search a list which contains data in the same page and if found
msg box in jsp
msg box in jsp  i need the source code to display the msgbox in jsp....but one condition is,i need to use it in the current page itself...now i use the message box means it prints it into another page...i need to resolve it plz
check box in jsp
check box in jsp  hey guys i am working on a web based project using jsp. In my project 1 am having 9 check boxes in 3 rows in the same form. I want to select 1 check box from each row and also i want to avoid many check box
HI Jsp check box..!
HI Jsp check box..!  Hi all.. I want to update the multiple values of database table using checkbox..after clicking submit the edited field has to update and rest has to enable to update...please help me..its urgent
how to crate callout box in jsp - JSP-Servlet
how to crate callout box in jsp  how to create a callout box when pointing a link in jsp page like in MS-word document callout box. Please help me I want to use
Scrolling List Box. - Java Beginners
Scrolling List Box.  How can is make a list box scrollable by using method ? Please give me the code snipetts
Scrolling List Box. - Java Beginners
Scrolling List Box.  How can is make a list box scrollable by using method ? Please give me the code snipetts
store values of drop down list box in database
store values of drop down list box in database  how to store values of drop down list box in oracle database in jsp?I have information inserting form where i have date of birth as drop down list box
combo box - JSP-Servlet
combo box  I am using a JSP form in which a combobox is given and I want to populate data in it from database and on the basis of yhe selected item I want to display related data in a textbox  Hi Friend, Create
JSP Text Box properties. - JSP-Servlet
JSP Text Box properties.  How do I restrict a text box created in JSP page only for numbers.. example: I have a function something like... any such in JSP. please let me know how to do it..  Hi Friend, Try
PHP list box mysql
PHP List box that displays the data from mysql table. User can select any value from the PHP list box. Example of PHP MYSQL List Box Code <?php if(isset($_GET['roll'])) { $username="root"; $password=""; $database
drop down box - JSP-Servlet
drop down box  when i enter some letter in the input box,the corresponding words of that particular letter must be displayed below as a list from the data base in the same input box as drop down. Thanks&Regards, VijayaBabu.M
PHP List Box Post
The PHP Post List box is used in the form. It contains multiple value User can select one or more values from the PHP list Box This PHP Post List Box is the alternate of Combo box PHP Post List Box Example <?php
PHP list box
The PHP List box is used in the form. It takes input values from the user. The PHP List box contains multiple values at a time. PHP List Box Example <?php      if(isset($_GET['hobbie']))   
Scrolling List Box. - Java Beginners
Scrolling List Box.  How can is make a list box scrollable by using... ListSelectionListener { private JList list; private DefaultListModel listModel..."); //Create the list and put it in a scroll pane. list = new
Passing Multi select list box values using ajax to get values to dependent list box
Passing Multi select list box values using ajax to get values to dependent list box  Hi, I have a jsp page which has 3 dependent list boxes... the dependent values to the immediate next list box. Please help me
Auto Complete Text Box - JSP-Servlet
Auto Complete Text Box  Please help me in Aotocomplete text box code. I m a java programmer. I m working on jsp tech. please give me the code for Autocomplete text box in which the values will generate from database table
How to retrieve data by using combo box value in jsp? - JSP-Servlet
How to retrieve data by using combo box value in jsp?  For example, In Employee.jsp form, When i click employee id value in combo box...... i already stored combo box values from database. pl
display from select box - JSP-Servlet
display from select box  I am doing a jsp project. In this jsp... selection box which is in the same page. The photos are stored...://www.roseindia.net/jsp/file_upload/ http://www.roseindia.net/jsp/file_upload
Use Ordered List In JSP
Use Ordered List In JSP   I created a Sting that contains ... . This string is generated dynamically from the db. When I place the string in the jsp, it does not create the ordered list. I can paste that string in the jsp
dynamic drop down list box - Java Beginners
dynamic drop down list box  hi all , I want to dynamically populate a drop down box from an sql query in a servlet program, using only html... Dynamic Combobox List
list to be displayed in jsp page.
list to be displayed in jsp page.  Can any one help me? Am developing the code in struts.I have a list in action class that has to be printed in the dropdownlist in another jsp page.Please help me in the code.. Thank u
How to retrieve data by using combo box value in jsp? - JSP-Servlet
How to retrieve data by using combo box value in jsp?  I am using a single jsp form.. i did not get from another form.. i do not use 2 jsp form.. all.... Note: I am using only this jsp form.. i do not use
dropdown list in jsp
dropdown list in jsp  hai, i have static dropdown list.. i want to get the selected value in string variable without jsp regards asha
how to get combo box value - JSP-Servlet
how to get combo box value  i have created 1 servlet & 1 jsp page. in servlet page i have fired query & fetched name & its id & that i have shown in combo box in jsp page: (small part of my code) "> now i want
populating text box using jsp code
populating text box using jsp code  Sir, How to populate related values in a text box after selecting value from drop down list using JSP and mysql.... Please give an example using jsp code only.   dependentDropdown.jsp
Auto Complete Text Box - JSP-Servlet
Auto Complete Text Box - JSP-Servlet  Auto Complete Text Box Please help me in Aotocomplete text box code. I m a java programmer. I m working on jsp tech. please give me the code for Autocomplete text box in which the values
How to retrieve data using combo box value in jsp? - JSP-Servlet
How to retrieve data using combo box value in jsp?  Hi freind, I already post this question. I need urgent help from u. pl response me... the following link: http://www.roseindia.net/jsp/comboSelect.shtml Hope
List of checkboxes - JSP-Servlet
List of checkboxes  Hi! I making a list of time-slots with a checkbox against every such time-slot in a JSP page. My requirement is that when one or more checkboxes are checked and the delete button is clicked
dependent drop down box - JSP-Servlet
the solution how to extract data from dependent drop down box with page refresh...; window.location.replace("http://localhost:8080/examples/jsp/dependentDropdown.jsp?id="+cid+"&&value...; window.location.replace("http://localhost:8080/examples/jsp/dependentDropdown.jsp
List the names of classes used to create button and text box in Java.
List the names of classes used to create button and text box in Java.  List the names of classes used to create button and text box in Java
How to retrieve data by using combo box value in jsp? - JSP-Servlet
How to retrieve data by using combo box value in jsp?  Hi Friends, When i click combobox value in jsp form page, it will diplay...... For Example, in Student.jsp... when i click student id in combo box
Automatically display in text box - JSP-Servlet
Automatically display in text box  Dear Deepak Sir, When i enter some text in text box.That corresponding value in database,the value display in another text box using ajax. Thanks & Regards, VijayaBabu.M
List of checkboxes - JSP-Servlet
List of checkboxes  Hi! I appreciate your effort for giving the response but now it displays only the last value of ArrayList. In other words, it is now displaying just the last time slot. Also, without clicking the checkbox
Iterate list of objects on jsp page
Iterate list of objects on jsp page  Hi, I want to iterate the list of objects on jsp page using custom tags. Can you please give me one link or example? Thanks
loading value into combo box by selecting value from other combo box - JSP-Servlet
loading value into combo box by selecting value from other combo box  ... is that as i select state from state_combo_box,then the next combo box is dynamically... box with constituency corresponding to district..... all three combo box
Name Display in alert box - JSP-Servlet
Name Display in alert box  Dear Sir, Please any one help me......... when i enter some value in text that value is already in database display the alert box but the value is not in data base alert box is not display
How to implement ajax in struts2 to operate on select box in jsp
by assigning to block list select box... plz help me .. its urgent .. thanx in advance...How to implement ajax in struts2 to operate on select box in jsp  I am doing a project on struts2 in which i have a jsp page which has 2 select boxes
To get the value of more than one text box in an HTML page to a jsp page - JSP-Interview Questions
To get the value of more than one text box in an HTML page to a jsp page  An html file has a text box as To get the value of this text box in a JSP... file has a text box as To get the value of this text box in a JSP file
How to get data from DB in to Text box by using Jsp & Ajax
How to get data from DB in to Text box by using Jsp & Ajax   I want to get the data from database in to text box in a jsp page by using Ajax. If I enter the letter A in the textbox it should show only those values which starts
To Retain the values entered in the text box after submit in jsp page
To Retain the values entered in the text box after submit in jsp page   i am working on a jsp pge which has many text boxes and one dynamic drop... box , data is retreived from the database and displayed in table format. I have
How to retreive data by using combo box value - JSP-Servlet
How to retreive data by using combo box value  Hello friend , i am using only one form. U are using html form and jsp form. So get...){ cid=Integer.parseInt(request.getParameter("cid")); I am using only a single jsp

Ads