Retain selected value in combo box after page getting refreshed

I have a jsp with a combo box with these options.

<select name="category">
            <option value="Shopping">Shopping</option>
            <option value="Sports" >Sports</option>
            <option value="Travel">Travel</option>
            </select>

After I select any of these options, I need to refresh the page, but the problem is that the selection is not getting saved after refrsh.  How do I retain the selection after refresh?  

dekathariAsked:
Who is Participating?
 
ManishConnect With a Mentor LeadCommented:
get the value in jsp ,
String category=request.getParameter("category"),
and check value with option value,
if match, write, selected.
ex.
<%
if(category.equals("Shopping")){ %>
<option value="Shopping" selected>Shopping</option>
<% } %>

0
 
ManishLeadCommented:
%
if(category.equals("Shopping")){ %>
<option value="Shopping" selected>Shopping</option>
<% } else {%>
<option value="Shopping">Shopping</option>
<%} %>
0
 
dekathariAuthor Commented:
This is what I tried,  but I am getting an exception

Duplicate local variable category

If I change the category variable to some other name, the error is gone, but the selecttion is not retained. Am I missingg something?

<select name="category" onChange="myForm.submit();">
            <option value="Shopping">Shopping</option>
            <option value="Sports" >Sports</option>
            <option value="Travel">Travel</option>
             </select>
             
             <%

              String category=request.getParameter("category");
             if(category.equals("Shopping")){ %>
            <option value="Shopping" selected>Shopping</option>
              <% } else {%>
          <option value="Shopping">Shopping</option>
          <%}
        %>
         
0
 
dekathariAuthor Commented:
Thanks a lot.

I have found the solution, this is what I did and is working fine.

<script>
function messageValue()
{
 
  document.myForm.submit();
}
 </script>

<%
String selected = (String)request.getParameter("category");
  if(selected == null)
     selected = "Shopping";   // default value
%>


<select name="category" onchange="messageValue()">
             <option value="Shopping" <%=selected.equals("Shopping")?"selected":""%> > Shopping</option>
             <option value="Sports" <%=selected.equals("Sports")?"selected":""%> > Sports</option>
            <option value="Travel" <%=selected.equals("Travel")?"selected":""%> > Travel</option>
</select>
 
0
All Courses

From novice to tech pro — start learning today.