简体   繁体   English

将servlet值传递到jsp页面

[英]passing servlet values to jsp page

I am trying to get values to a drop down list based on the previous value selected from another drop down list. 我试图根据从另一个下拉列表中选择的上一个值将值添加到下拉列表中。 I am able to call to java class from the servlet and the values are returned to servlet but it is not getting passed down to jsp page 我能够从Servlet调用Java类,并将值返回给Servlet,但它不会传递给JSP页面

jsp page part(index.jsp) jsp页面部分(index.jsp)

<select id="region" name="region" class="form-control" onchange="getgroups()" required="required" >
<option value="" default selected>Select region</option>
<option value="region1">region1</option>
<option value="region2">region2</option>
<option value="region3">region3</option>
<option value="region4">region4</option></select>

The onchange value in region calls this function 区域中的onchange值调用此函数

function getgroups(){

var j = document.getElementById("region");
var s = document.getElementById("secret_key");
var a = document.getElementById("access_key");

   var output = $.ajax({
     type: 'POST',
     url: 'http://localhost:8066/VMMigratorNew/ec2util',
     data: {"region":j.value,
            "secret_key":s.value,
            "access_key":a.value,
            },
     success: function(response) {
         return output.responseText;
     }

});

}

and in servlet 并在servlet中

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String region = request.getParameter("region");
        String secretKey = request.getParameter("secret_key");
        String accessKey = request.getParameter("access_key");




            List<String> vpcs = RunEc2Command.getVPCForUSer(AWS_ACCESS_KEY, AWS_SECRET_KEY, region);

            if(vpcs != null && vpcs.size() > 0) {
                                request.setAttribute("vpclist", vpcs);
            }

        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }
}

and in the same index.jsp page , I am trying to show the list values in a drop down box which is in index.jsp itself. 在同一index.jsp页面中,我试图在index.jsp本身的下拉框中显示列表值。

          <select id="vpc" name="vpc" class="form-control" placeholder="Choose VPC"  >
                                        <option value="" default selected>Select VPC</option>

                                            <c:forEach items="${vpclist}" var="vpcs">    
                                                 <option>
                                                   ${vpcs}
                                                 </option>
                                            </c:forEach> 
                                    </select> 

I need to get the value of VPC in drop dron box after selecting the value from region drop down box. 从区域下拉框中选择值后,需要在下拉dron框中获取VPC的值。 Thanks for the help in advance. 我在这里先向您的帮助表示感谢。

Currently, the workflow in your servlet is suitable for a synchronous request, which requires the servlet to return the whole HTML in the HTTP response for the browser to render. 当前,您的servlet中的工作流适合于同步请求,这要求servlet在HTTP响应中返回整个HTML,以便浏览器呈现。 However, it seems to me that your intent is instead of querying the servlet in order to obtain a VPC list, and you are trying to pose the query asynchronously using AJAX. 但是,在我看来,您的意图不是查询servlet以获得VPC列表,而是尝试使用AJAX异步提出查询。

Hence, my suggestion would be to let the servlet return the VPC list in some format (eg, JSON). 因此,我的建议是让servlet以某种格式(例如JSON)返回VPC列表。 In the success callback, you should modify the DOM in order to insert the list you received. success回调中,您应该修改DOM以便插入收到的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM