How to keep selected value in dropdown box after form submission?

15,022

Solution 1

You need to pass this parameter to the httpRequest after submitting:

request.setAttribute("selectedModule", request.getParameter("chosenOne"));

and after that you need to mark an option as selected:

<c:forEach items="${modules}" var="module"> 
     <option value="${module.id}" ${module.id == selectedModule ? 'selected':''}>...</option>  
</c:forEach> 

Solution 2

I can explain like this. As a example code put this code in your Servlet

String status = request.getParameter("status");
request.setAttribute("status", status);

put this code in jsp file

<select  id="status" name="status" class="listBx"  onChange = "check(this);">
    <option value="" >--- Select ---</option>                           
    <option value="1"  <%if((request.getAttribute("status") != null) && request.getAttribute("status").equals("1")){ %> selected <%} %>>Logged in</option>
    <option value="0"  <%if((request.getAttribute("status") != null) && request.getAttribute("status").equals("0")){ %> selected <%} %>>Logged Out</option>
</select>
Share:
15,022
Admin
Author by

Admin

Updated on June 11, 2022

Comments

  • Admin
    Admin almost 2 years

    Out of the many solutions I've found, I can't seem to get any of them to work for me. I've got a dropdown list in my jsp file:

        <select name="chosenOne" onchange="javascript:getUsers(this.value);">  
                <option value="0" onclick="javascript:getUsers(this.value);">All Modules</option>
                <c:forEach items="${modules}" var="module"> 
                    <option value="${module.id}"><c:out value="${module.title}"/></option>  
                </c:forEach>  
            </select></p> 
    

    It populates dynamically from my database, except for the "All Modules" option. Here's my javascript function for the onchange event:

           <script type="text/javascript">
            function getUsers(id) { 
                if (id != "0"){
                document.updateForm.id.value = id;
                }
                else{
                document.updateForm.id.value = "0";
                }
                document.updateForm.submit(); 
            }</script>
    

    And here's my servlet code that deals with the dropdown box amongst other things:

            protected void process(final HttpServletRequest request, final HttpServletResponse response)
            throws ServletException, IOException {
    
        long modID = 0;
        String url = "jsp/user/administration.jsp";
        request.setAttribute("modules", dataAccessor.getAllModules());
    
        if (isParameterValid(request.getParameter("id"))) {
            modID = Long.parseLong(request.getParameter("id"));
            request.setAttribute("users", getUsersFromModule(modID));
            System.out.println(modID);
    
        } else if (!isParameterValid(request.getParameter("id"))) {
            request.setAttribute("users", dataAccessor.getAllUsers());
    
        } else {
            request.setAttribute("errorMessage", "There was a problem retrieving users!");
            url = "jsp/error.jsp";
        }
    
        //request.setAttribute("formerSelect", modID);
        request.getRequestDispatcher(url).forward(request, response);
    }
    

    So how can I get the selected dropdown value to remain in the dropdown box after the form refreshes? I've fiddled around with setting an attribute "formerSelect" which just contains the value of the previously selected item in the dropdown. But then for some reason it rendered my dropdown useless when I tried to assign it to the "selected" value within my options tag. Any help is much appreciated.