使用servlet填充另一个更改的下拉列表

时间:2012-01-02 05:59:08

标签: ajax servlets drop-down-menu

我是java struts中的新手我正在开发struts 1.3中的web应用程序我有两个下拉列表,一个是位置,另一个是楼层,我有一个要求,在更改一个下拉值的其他下拉列表填充从数据库为我google了很多,我得到了代码,但当我改变我的第一个下拉列表第二个下拉列表不填充虽然我在Netbeans的调试模式中看到该值从数据库返回.i在servlet doGet方法中执行我的数据库活动

 <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代码

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

和web.xml中的servlet映射

web.xml

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

1 个答案:

答案 0 :(得分:0)

在servlet中,编写response.getWriter()。write(buffer)而不是response.getWriter()。println(),并尝试提醒你从servlet获得的响应 在你写的ajax代码中。好像你的javascript没有收到响应。 如果问题没有解决,那么我在线。