简体   繁体   English

在jsp中从下拉选择中调用参数值设置为java方法

[英]Calling a java method with parameter value set from dropdown select in jsp

My task is to select a value in one dropdown, and with that value as a parameter, invoke a java method. 我的任务是在一个下拉列表中选择一个值,然后将该值作为参数,调用java方法。

I tried setting a hidden input when via onChange, a javascript function is called, but could not use that value for passing as a parameter. 我尝试通过onChange设置一个隐藏的输入,调用了javascript函数,但是无法使用该值作为参数传递。 (I have a bean, that has the method which i need to invoke from jsp after selecting value from dropdown) (我有一个bean,它具有从下拉列表中选择值后需要从jsp调用的方法)

You can make Ajax call to the servlet with XMLHttpRequest object in JavaScript. 您可以使用JavaScript中的XMLHttpRequest对象对servlet进行Ajax调用。

You can make a successful call to servlet as: 您可以通过以下方式成功调用servlet:

<script>
    var req = new XMLHttpRequest();
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            var data = req.responseText;
            //HANDLE RESPONSE HERE;
        }
    }
    req.open('GET', 'servletName', true);
    req.send(null);
</script>

In servlet, handle the parameter passed from dropdown in request and accordingly, call java method and send response text as: 在servlet中,处理从request中的dropdown传递的参数,并相应地调用java方法并将响应文本发送为:

String responseData = "Output for your selection is : " + XXXX + "!";
response.setContentType("text/plain");
response.getWriter().write(responseData);

Test crossbrowser compatibility before using it. 使用它之前,请测试跨浏览器的兼容性。

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

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