• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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: 17734
302
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?
 
His brain is the size of a cherry pit! About the size of this ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic