0

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> 
home
  • 12,468
  • 5
  • 46
  • 54
zohaib siddiqui
  • 129
  • 1
  • 6
  • 18

1 Answers1

0

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.

Acn
  • 1,010
  • 2
  • 11
  • 22
  • i checked by using response.getWriter().write(buffer) but one thing i came to know that when i alert the response its bank java script is not receiving the response though i checked response System.out.println(buffer); response is there on console – zohaib siddiqui Jan 02 '12 at 08:15
  • he he he ha ha ha you do this thing then - http.onreadystatechange = handleResponse**(http)** you didn't pass the parameter http. How would the handle response know what this http is ? this will work now. – Acn Jan 02 '12 at 08:55
  • i did http.onreadystatechange = handleResponse(http);function handleResponse() { alert(http.status); if(http.readyState == 4 && http.status == 200) { var response = http.responseText; alert(response); if(response) { document.getElementById("dd2").innerHTML = response; } } } now not entering in this function – zohaib siddiqui Jan 02 '12 at 09:20
  • I am sorry this time. Edit - http.onreadystatechange = function() { handleresponse(http); } this wll work sure. – Acn Jan 02 '12 at 09:32
  • sorry to disturb you i change this line http.onreadystatechange = function() { handleresponse(http); } ; i did but still response coming blank or null shows in alert window or have to change in this function handleresponse() defination also – zohaib siddiqui Jan 02 '12 at 09:52
  • http.open(method,url); -> replace this by http.open("GET", url, true);I donot have more idea why this is happening :( . – Acn Jan 02 '12 at 09:59
  • i wrote the same code of servlet populating the second dropdown in a jsp file its working fills the second drop down but not understanding why not working with Servlet .... reason unknown so far...stil struggling – zohaib siddiqui Jan 02 '12 at 10:27
  • This is not a forum, nor is it a real-time communication medium - please phrase your answers as actual answers, and use the edit link to update your existing answer. If you want to start an informal discussion based on this question, consider creating a chat room at http://chat.stackoverflow.com – BoltClock Jan 03 '12 at 08:21