• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting the selected option

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic