title: populate dropdown on change of another using servlet
body: <p>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 .i do my database activity in servlet doGet method</p>
  
  <pre><code> &lt;script&gt;
   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 &amp;&amp; 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 );
   }
  
   &lt;/script&gt;
  
   &lt;tr&gt;
   &lt;td &gt;
   &lt;span style="color:#FF0000"&gt;*&lt;/span&gt;Location&lt;/td&gt;
   &lt;td&gt; &lt;html:select name="RoomForm" property="name"
   onchange="getFloorDropdown(this.value)"&gt;
   &lt;htmlption value="0"&gt;Select Location&lt;/htmlption&gt;
   &lt;htmlptionsCollection name="RoomForm"
   property="list" value="id" label="name" /&gt;
   &lt;/html:select&gt;
   &lt;td&gt;
   &lt;/tr&gt;
   &lt;tr&gt;
   &lt;td &gt;
    &lt;span style="color:#FF0000"&gt;*&lt;/span&gt;Floor
   &lt;/td&gt;
  &lt;td id="dwnfloor"&gt;
  
  &lt;select name="dwnfloor"&gt;
  &lt;option value="0"&gt;Select Floor&lt;/option&gt;
  &lt;/select&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  </code></pre>
  
  <hr>
  
  <p><strong>Servlet Code</strong></p>
  
  <pre><code>protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
      processRequest(request, response);
      String country=request.getParameter("locid");
      String buffer="&lt;select name=\"dwnfloor\"&gt;&lt;option value=\"0\"&gt;Select&lt;/option&gt;";
      Connection connection = null;
      PreparedStatement p_statement = null;
      Statement statement = null;
      ResultSet result = null;
  
      try {
          DAOOperation dao= new DAOOperation();
          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+"&lt;option value=\""+result.getString("ID")+"     \"&gt;"+result.getString("name")+"&lt;/option&gt;";
              }
  
              buffer=buffer+"&lt;/select&gt;";
              response.getWriter().println(buffer);
              System.out.println(buffer);
          } catch (Exception e) {
              e.printStackTrace();
          } finally {
              try {
                  connection.close();
              } catch (Exception e) {
              }
          }// end finally
      } catch(Exception e) {
          System.out.println(e);
      }
  }
  </code></pre>
  
  <hr>
  
  <p><strong>and servlet mapping in web.xml</strong></p>
  
  <p>web.xml </p>
  
  <pre><code>&lt;servlet-mapping&gt;
  &lt;servlet-name&gt;DropDown&lt;/servlet-name&gt;
  &lt;url-pattern&gt;/DropDown&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt; 
  </code></pre>