I am doing program in java .I have designed a jsp page which has 3 drop downlist's...country,state and district.I want to display the values in drop down list state when a country is selected...(for eg:-if india is selected,drop down list state must have only the name of states in india).In Mysql i have created 3 table's...table country has a primary key and country name.table state have state id,state name and a foreign key from country id...What is the code in servlet and jsp to do this?Pls help me to do this pblm...

Check this out
This is how i implemented it .
first i created two tables in database named as country and state
1)create table country(countryid number(6),countryname varchar2(30));
2)create table state(stateid number(6),statename varchar2(30),countryid number(6));
then i wrote simple jsp code to retrieve data from database which populates data of country table and then i called javascript function showState which gets data from getState.jsp... see how create two JSP pages
getcountry.jsp and getstate.jsp separetly pages that worked from database tables if you have filled correctly

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
pageEncoding="ISO-8859-1"%> 
<%@ page import="java.sql.*"%> 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Populate Using Ajax</title> 
<script> 
function showState(str){ 
var xmlhttp; 

if (window.XMLHttpRequest) 
{// code for IE7+, Firefox, Chrome, Opera, Safari 
xmlhttp=new XMLHttpRequest(); 
} 
else 
{// code for IE6, IE5 
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
} 
xmlhttp.onreadystatechange=function() 
{ 
if (xmlhttp.readyState==4 && xmlhttp.status==200) 
{ 
document.getElementById("state").innerHTML=xmlhttp.responseText; 
} 
} 
xmlhttp.open("GET","getstate.jsp?q="+str,true); 
xmlhttp.send(); 
} 
</script> 


</head> 
<body> 
Country : <select onchange="showState(this.value)"> 
<% 
try { 
Class.forName("oracle.jdbc.OracleDriver"); 
} catch (ClassNotFoundException e) { 
e.printStackTrace(); 
return; 
} 
Connection connection = null; 
try { 

connection = DriverManager.getConnection("jdbcracle:thin:@localhost:1521rcl","scott","tiger"); 
} 
catch (SQLException e) { 
e.printStackTrace(); 
return; 
} 

PreparedStatement stmt=null; 
stmt=connection.prepareStatement("select * from country"); 
ResultSet rs=null; 
rs= stmt.executeQuery(); 

while(rs.next()){ 
%> 

<option value="<%=rs.getString(1)%>"><%=rs.getString(2)%></option> 
<% 
} 
%> 
</select> 
<div id="state"> 
State : 
<select> 
<option>Select State</option> 
</select> 
</div> 
</body> 
</html> 

see getstate.jsp 
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
pageEncoding="ISO-8859-1"%> 
<%@ page import="java.sql.*"%> 
<%!int i;%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>State Page</title> 
</head> 
<body> 
State : 
<select> 
<% 
String str=request.getParameter("q"); 

i=Integer.parseInt(str); 
try { 
Class.forName("oracle.jdbc.OracleDriver"); 
} catch (ClassNotFoundException e) { 
e.printStackTrace(); 
return; 
} 
Connection connection = null; 
try { 

connection = DriverManager.getConnection("jdbcracle:thin:@localhost:1521rcl","scott","tiger"); 
} 
catch (SQLException e) { 
e.printStackTrace(); 
return; 
} 

PreparedStatement stmt=null; 
stmt=connection.prepareStatement("select * from state where countryid='"+i+"'"); 
ResultSet rs=null; 
rs= stmt.executeQuery(); 

while(rs.next()){ 
%> 
<option value="<%=rs.getString(1)%>"><%=rs.getString(2)%></option> 
<% 
} 
%> 
</select> 

</body> 
</html> 
This worked for me i hope it will work for all those who are looking to retrieve data using ajax in jsp.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.