How to take the value of a select option and popular within a jsp variable

Asked

Viewed 77 times

0

I would like to know how I get the value of select option and inputo within a variable in jsp. I’m trying the following code plus this giving error :

Here I fill select with data coming from the database.

<select id="STR_MAC" style="width: 600px;" name="STR_MAC" onchange="update()">

                        <%
                            String qry_types2 = "SELECT DISTINCT MACROTECNOLOGIA FROM EPE_VW_PRT_MAPE_ALLOC ORDER BY MACROTECNOLOGIA";
                        ResultSet rsSTR_MAC = stm.executeQuery(qry_types2);

                        while (rsSTR_MAC.next()) {
                            if (rsSTR_MAC.getString("MACROTECNOLOGIA").equals(request.getParameter("STR_MAC"))) {
                                out.println("<option value='" + rsSTR_MAC.getString("MACROTECNOLOGIA") + "' selected=true>"
                                + rsSTR_MAC.getString("MACROTECNOLOGIA") + "</option>");
                            } else {
                                out.println("<option value='" + rsSTR_MAC.getString("MACROTECNOLOGIA") + "' >"
                                + rsSTR_MAC.getString("MACROTECNOLOGIA") + "</option>");
                            }
                        }
                        rsSTR_MAC.close();
                        %>
                    </select>

And here’s where I’m trying to pass the value of the select option to my variable but this is giving error.

<script type="text/javascript">
    <% 
    String STR_MAC = new String();
    
        function update() {
            var select = document.getElementById('STR_MAC');
            var text = select.options[select.selectedIndex].text;
            console.log(text);
        }
        update();
        
        STR_MAC = text;
        
        System.out.println("Esse eu peguei da tela : " + STR_MAC);
        %>
    </script>

Here follows the error that is being generated on my page :

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 161 in the jsp file: /kpi/chart_people_knowledge_risk.jsp
Syntax error on token "function", new expected
158:    <% 
159:    String STR_MAC = new String();
160:    
161:        function update() {
162:            var select = document.getElementById('STR_MAC');
163:            var text = select.options[select.selectedIndex].text;
164:            console.log(text);

Thank you all and thank you for your attention.

1 answer

0

You’re mixing JSP with javascript;

<script type="text/javascript">

    function update() {
        var select = document.getElementById('STR_MAC');
        var text = select.options[select.selectedIndex].text;
        console.log(text);
    }
    update();
</script>

As far as I can remember, you can’t pass a javascript variable to scriptlet, so the code below won’t work;

STR_MAC = text;
    
    System.out.println("Esse eu peguei da tela : " + STR_MAC);
  • And in this case how could I take this value from the select option and pass to a jsp variable ?

  • No onchange makes a request using fetch api passing the value, so you can catch.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.