0

I have a dropdown list in a jsp file

<select name="PID" id="PID" style="min-width: 100px;" >

<% 
for(somelist pm : List) { %>
<option value="<%=pm.getsmthing()%>" selected><%=pm.getName()%></option>
<option  value="-1">Select a Property</option>

This is inside a form. I want to remember the selected item name after submission of the form.

2 Answers2

1

Just print selected conditionally whenever the associated request parameter has exactly the same value.

<option value="<%=pm.getsmthing()%> <%=(pm.getsmthing().equals(request.getParameter("PID")) ? "selected" : "")%>><%=pm.getName()%></option>

Do not store it in the session. It would affect all pages in all browser tabs/windows in the same session which may result in "wtf?" experiences of the enduser.


Unrelated to the concrete problem, that's pretty an oldschool way of writing JSPs. Learn JSTL and EL. Your code would then look like this:

<select name="PID" id="PID" style="min-width: 100px;">
  <c:forEach items="${List}" var="pm">
    <option value="${pm.smthing}" ${pm.smthing == param.PID ? 'selected' : ''}>${pm.name}</option>
    <option value="-1">Select a Property</option>
  </c:forEach>
</select>
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

Add your choosen value to HTTP session and retrive it anytime you need to show your form.

Piotr Kochański
  • 21,862
  • 7
  • 70
  • 77