populate dropdown on change of another using servl

2019-08-24 08:41发布

问题:

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

 <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)">
 <htmlption value="0">Select Location</htmlption>
 <htmlptionsCollection 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>

Servlet Code

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();
        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();
        } 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> 

回答1:

In the servlet, write response.getWriter().write(buffer) instead of response.getWriter().println() and also, try to alert the response you got from the servlet in the ajax code you have written. It seems like your javascript has not recieved the response. If the problem is not solved then Im online.