Solution 1 :

In JSP you should have something like that:

<form method="post">
    <select name="taxiDropdown" id="taxiDropdown">
        <%
            List<TaxiInfo> eList = (List<TaxiInfo>) request.getAttribute("taxiInfo");
            for (TaxiInfo taxiInfo : eList) {
        %>
        <option name="<%=taxiInfo.getTaxiPlate()%>" value="<%=taxiInfo.getID()%>"><%=taxiInfo.getTaxiPlate()%></option>
        <%
            }
        %>
    </select>
    <input type="submit" />
</form>

Then in controller/servlet you will receive the id of TaxiInfo:

String val = request.getParameter("taxiDropdown");
System.out.println(val);

Or in your case you should set a hidden input with javascript with desired value.

Solution 2 :

added this code in html:

Move value of the dropdown selection to hidden textbox

<script type='text/javascript'>
    $(function() {
        $('#driverdp').change(function() { <-- this is my dropdown -->
            var x = $(this).val();
            $('#driverid').val(x); <-- this is my textbox -->
        });
    });
</script>

Servlet:
get the value of hidden text in servlet

String text= request.getParameter("driverId");

hope it will help someone

Problem :

I have dropdown with options and the values. I can get the option value by the dropdown name in servlet but how can i get the dropdown “value” in servlet. In screenshot, temporarily i concatenated the with options but i want to store value in variable in servlet.
Please help:

HTML:

 <input type="text" name="taxiDropdown" id= "taxiDropdown" placeholder="Search taxi...">
                            </div>
                                <div class="scrolling menu">
                                    <%
                                        List eList = (ArrayList) session.getAttribute("taxiInfo");
                                    %>
                                    <%
                                        for (int i = 0; i < eList.size(); i++) {
                                    %>
                                    <div class="item" data-value="<%=((TaxiInfo) eList.get(i)).getID()%>">
                                        <div class="ui green empty circular label"></div>
                                        <%=((TaxiInfo) eList.get(i)).getTaxiPlate() +" "+ ((TaxiInfo) eList.get(i)).getID() %>

                                    </div>
                                    <%
                                        }
                                    %>
                                </div>
                            </div>

Servlet:

String val = request.getParameter("taxiDropdown");

(in “val”, I want to store the value of the dropdown not the option text)

enter image description here

Comments

Comment posted by Swati

do you need

Comment posted by Eddie

Absolutely right, how can i get that? so dropdown option giving me 1076(name in table) and value give me (1(ID in table)

Comment posted by Swati

when you select any option from dropdown what does

Comment posted by Eddie

Thanks, another person said, i couldn’t get the “Value”(input name=”text” value=”123″) without putting in html. As you see in brackets, i am trying to get “123” with getParameter which is not possible i think.

Comment posted by Eddie

so you saying, there is no way to get value=”<%=taxiInfo.getID()%>“> in request.getParameter without hidden input?

Comment posted by Getodac

Yes, as I know, you can’t get the value of taxiInfo.ID without using a classic html select, or with a hidden input that you set with javascript.

Comment posted by Eddie

Good to know, i added hidden field and get the value. thank you

By

Leave a Reply

Your email address will not be published. Required fields are marked *