<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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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>

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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>

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.