Passing a variable value from javascript to JSP - Stack Overflow

I have the following scenario<select name="dropdown_Source" onchange="call()">

I have the following scenario

<select name="dropdown_Source" onchange="call()">
    <% for (int i=0 ; i < a1.length; i++) { if (a1[i]==n ull) break; %>
        <option value="<%=a1[i]%>">
            <%=a1[i]%>
        </option>
    <% } %>
</select>

<script>
    function call() {
        var source = document.forms[0].dropdown_source.value;
        // Now this 'source' value i have to pass to jsp function 
        <%
            Admin a = new Admin(); //this is class defined
            a.getResult(source); //but source is not resolved here
        %>
    }
</script>

How this source value should I pass in JSP function? a.getResult() - function will return me list of dropdown elements and that list I have to populate in another <option> tag.

I have the following scenario

<select name="dropdown_Source" onchange="call()">
    <% for (int i=0 ; i < a1.length; i++) { if (a1[i]==n ull) break; %>
        <option value="<%=a1[i]%>">
            <%=a1[i]%>
        </option>
    <% } %>
</select>

<script>
    function call() {
        var source = document.forms[0].dropdown_source.value;
        // Now this 'source' value i have to pass to jsp function 
        <%
            Admin a = new Admin(); //this is class defined
            a.getResult(source); //but source is not resolved here
        %>
    }
</script>

How this source value should I pass in JSP function? a.getResult() - function will return me list of dropdown elements and that list I have to populate in another <option> tag.

Share Improve this question edited May 11, 2013 at 12:40 Vitalii Petrychuk 14.3k8 gold badges54 silver badges55 bronze badges asked May 11, 2013 at 12:27 Rachana KRachana K 1041 silver badge4 bronze badges 1
  • ugly code.. I dont know jsp but I am sure this will not work because jsp server-side javascript client-side.. If you can get dropdown value with jsp do it.. With asp I can get value of dropdown which I set its value from javascript. – Mehmet Commented May 11, 2013 at 12:33
Add a ment  | 

3 Answers 3

Reset to default 3

You cannot do that. JavaScript executes on the client side after JSP page is already piled and sent to the browser as response.

You can not mix javascript and java code. when you do

<%
            Admin a = new Admin(); //this is class defined
            a.getResult(source); //but source is not resolved here
        %>

This means that code snippet will be processed at server side as this is plain java code.Now variable "source" is not java variable but a javascript variable which will e into picture when your html page loads on browser. So its a pilation error.

The best thing to do here is, once the javascript executes, take that source value and make an AJAX call to your server, process it and send the values back to the client and then populate your dropdown.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742417968a4440111.html

相关推荐

  • Passing a variable value from javascript to JSP - Stack Overflow

    I have the following scenario<select name="dropdown_Source" onchange="call()">

    22小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信