使用AJAX将选择框从servlet发送到JSP

时间:2015-04-11 18:23:47

标签: ajax jsp servlets

我正在获取公司名称选择框的动态数据。基于这些值,我需要为“业务范围”选择框填充数据。为此,我通过AJAX调用servlet。

在servlet中,我根据公司名称获取业务区列表,但我无法理解如何将此列表传递给我的JSP代码。

我正在为选择框获取动态数据,如下所示:

公司名称:

<select id="company_id" onchange="getBusinessArea()">
    <option selected="selected">--Select One--</option>
    <% for (String txt : new ExtractDao().getCompanies()) {%>
    <option><%=txt%></option>
    <%}%>
</select>

通过使用上述公司名称,我需要填写以下选择框的数据:

Business Area :
 <select>
    <option><option>
 </select>

我使用AJAX调用我的servlet:

function getBusinessArea() {
        var elem = document.getElementById("company_id");
        var selectedNode = elem.options[elem.selectedIndex].value;
        window.alert(selectedNode);
        var xmlhttp;
        var companyData = "${pageContext.request.contextPath}/ExtractController?companyName="
                + selectedNode;

        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        } else {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }

        window.alert(companyData);
        xmlhttp.open("GET", companyData, true);
        xmlhttp.send();
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                alert(http_request.responseText);
            } else {
                alert('There was a problem with the request.');
            }
        }
    }

我的Servlet代码:

public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        ExtractService extService = null;
        PrintWriter out = response.getWriter();
        response.setContentType("text/html");
        String companyName = request.getParameter("companyName");
        extService = new ExtractService();
        List<String> bList = extService.getBusinessAreas(companyName);
    }
}

有人可以帮我展示如何为业务范围选择框填充数据吗?

0 个答案:

没有答案