• 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

populate one drop down on change of other using ajax and servlet with jsp page

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am new in java struts i am developing web application in struts 1.3 i have two dropdowns one is for location and another is for Floor,i have a requirement that on change on one dropdown values of other dropdown fills from database for i googled a lot and i got code but when i change on my first dropdown second dropdown does not populate though i saw in debugging mode in Netbeans that that values return from database my sample code is this

my JSP page

<script>
function createRequestObject()
{

var req;

if(window.XMLHttpRequest)
{
//For Firefox, Safari, Opera
req = new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
//For IE 5+
req = new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
//Error for an old browser
alert('Your browser is not IE 5 or higher, or Firefox or Safari or Opera');
}

return req;
}

//Make the XMLHttpRequest Object
var http = createRequestObject();

function sendRequest(method, url)
{

if(method == 'get' || method == 'GET')
{

http.open(method,url);
http.onreadystatechange = handleResponse;
http.send(null);
}
}

function handleResponse()
{

if(http.readyState == 4 && http.status == 200)
{

var response = http.responseText;
if(response)
{

document.getElementById("dwnfloor").innerHTML = response;
}
}
}

function getFloorDropdown(SelectedValue)
{
alert(SelectedValue);
sendRequest('GET','http://localhost:8084/AssetManagement/DropDown?locid=' +SelectedValue );
}

</script>

<tr>
<td >
<span style="color:#FF0000">*</span>Location</td>
<td> <html:select name="RoomForm" property="name"
onchange="getFloorDropdown(this.value)">
<html:option value="0">Select Location</html:option>
<html:optionsCollection name="RoomForm"
property="list" value="id" label="name" />
</html:select>
<td>

</tr>
<tr>
<td >
<span style="color:#FF0000">*</span>Floor
</td>
<td id="dwnfloor">

<select name="dwnfloor">
<option value="0">Select Floor</option>
</select>


</td>

</tr>

my Servlet

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);

String country=request.getParameter("locid");
String buffer="<select name=\"dwnfloor\"><option value=\"0\">Select</option>";

Connection connection = null;
PreparedStatement p_statement = null;
Statement statement = null;
ResultSet result = null;

try{
DAOOperation dao= new DAOOperation();
/* ResultSet rs=dao.PopulateDropDown(country);*/
String sqlst = "select id,name from floor_mst where id=?";


try {
connection = DBConnection.getConnection();
p_statement = connection.prepareStatement(sqlst);
p_statement.setString(1, country);

result = p_statement.executeQuery();
while(result.next())
{
buffer=buffer+"<option value=\""+result.getString("ID")+"\">"+result.getString("name")+"</option>";
}
buffer=buffer+"</select>";
response.getWriter().println(buffer);
System.out.println(buffer);

} catch (Exception e) {
e.printStackTrace();
}// end of catch

finally {
try {
connection.close();
} catch (Exception e) {
}
}// end finally



}
catch(Exception e){
System.out.println(e);
}

}
and servlet mapping in web.xml

web.xml

<servlet-mapping>
<servlet-name>DropDown</servlet-name>
<url-pattern>/DropDown</url-pattern>
</servlet-mapping>

where i am doing wrong i am not understanding please any body help
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am also facing the same problem can any one help!
 
reply
    Bookmark Topic Watch Topic
  • New Topic