Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting the selected option  RSS feed

 
Lance Jurgensen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have a pair of jsp pages and I want to maintain the state of the fields between the pages.

How do you reset the "selected" value in an options drop down box?

I have the value stored on page two like this:
<input type="hidden" name="suff1" <%if(request.getParameter("suff1")!=null){%>value="<%=request.getParameter("suff1")%>"<%}%>>

On page one I want to get the selected option, I tried this but it doesn't work:
<select name="suff1">
<option value="AM" <%if(request.getParameter("suff1")!=null && request.getParameter("suff1")=="AM"){%>selected<%}%>>AM</option>
<option value="PM" <%if(request.getParameter("suff1")!=null && request.getParameter("suff1")=="PM"){%>selected<%}%>>PM</option>
</select>

Thanks
 
Junilu Lacar
Sheriff
Posts: 11146
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use the expression

"AM".equals(request.getParameter("suff1"))

instead of

request.getParameter("suff1")=="AM"


I would prefer JSTL over the scriptlet though:

<option value="AM"
<c:if test="${param.suff1 == 'AM'}">selected</c:if>>AM</option>

<option value="PM"
<c:if test="${param.suff1 == 'PM'}">selected</c:if>>PM</option>

Now, isn't that much cleaner?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!