<select> field in jsp

Dear experts

I have a select field and the value of the select field is set in request in (request.getAttribute("stateSel") )and
i am trying to display that value in jsp but its not working. I want the value to be selected "BY default" when the jsp
is opened

here is the code i have


<select name="state" tabindex="7" class="formstyle" onchange="Options();" width="156" style="width:156px" >
                         
                          <option value="<%=(String)request.getAttribute("stateSel")%>" selected="selected"></option>
                       
                        </select>
                         
any help greatly appreciated

thanks
J
LVL 10
jaggernatAsked:
Who is Participating?
 
WelkinMazeCommented:
Hi,

Try this one:

<select name="state" tabindex="7" class="formstyle" onchange="Options();" width="156" style="width:156px" >
                       
                          <option value="<%=(String)request.getAttribute("stateSel")%>" selected><%=(String)request.getAttribute("stateSel")%></option>
                       
                        </select>
0
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
Make a null check its a better approach.

<%
String state =(String)request.getAttribute("stateSel");
if(state==null)
{
state="";
}
%>

<select name="state" tabindex="7" class="formstyle" onchange="Options();" width="156" style="width:156px" >
                       
                          <option selected value='<%= state%>' ><%=state%></option>
           
</select>

0
 
sompol_kiatkamolchaiCommented:
I think you may have a collection of data to be displayed in the select list box.
If so, this would be closer to your answer.

<select name="state" tabindex="7" class="formstyle" onchange="Options();" width="156" style="width:156px" >
<% for(int i=0; i<list.size(); i++) {
        String opt = (String)list.elementAt(i);
%>
    <option value="<%=opt%>" <%if(((String)request.getAttribute("stateSel")).equalsIgnoreCase(opt)){%>selected<%}%>><%=opt%></option>
<% } %>
</select>

Hope this help.
Sompol
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.