In my project. I want to populate the drop down list on a jsp from a database.
<select id="names" name="names"> <c:forEach items="${names}" var="names">
<option><c:out value="${names}"/></option>
</c:forEach>
</select>
The ${names} is a list of names from the database. I want to select an option dynamically in the drop down list. Suppose there are three names in the database Rohan, Dean, Justin. If Dean is logged, i want select the option Dean as selected.
I try a code like this but this does not work.
<option value="${names}" ${names == names ? 'selected' : ''}>${names}</option>
equals()not with==names == namesis useless...namesvar toname, just for easier maintenance. Second, you're comparingnameswithnames(and you should useequalsto compareStringvariables), like1 == 1, you should compare thenamevariable with a request or session parameter that handles the actual userName or another variable you want/need.