Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 335
  • Last Modified:

how to set default value for drop downlist

<select name="state">
<option value='red'>RED</option>
<option value='blue'>BLUE</option>
</select>

i mean for text it is like this
<input type="text" name="username" value=<%=vol.getUser()%> >

but how is it for drop down list

thanks
0
Jasbir21
Asked:
Jasbir21
  • 6
  • 5
  • 2
  • +1
3 Solutions
 
jimmackCommented:
If you mean that you want (for example) BLUE to be selected in the dropdown by default, use:

<option value='blue' selected>BLUE</option>
0
 
Jasbir21Author Commented:
actually, the user would select something, and it could either be blue,yellow, red and etc.
On the update page, the default value, value selected by user should be chosen.

Thanks
0
 
jimmackCommented:
I'm not quite sure why my comment doesn't answer your question.  Unless...  ;-)

To do this programmatically (creating a page when the user has made a selection from the list and this is present in the request):

<%
String[] colorText = {"RED", "BLUE", "YELLOW"};
String[] colorValue = {"red", "blue", "yellow"};
String selectedColor = request.getParameter("state");
%>
<select name="state">
<%
for (int i = 0; i < colorText.length; i++)
{
%>
<option value='<%= colorValue[i] %>'
<%
    if (colorValue[i].equals(selectedColor))
    {
        out.print(" selected");
    }
%>
><%= colorText[i] %></option>
<%
}
%>
</select>

Is this closer ?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Jasbir21Author Commented:
sorry, for confusing,
when i put this:

<option value='blue' selected>BLUE</option>
blue is always selected by default, i look up at the dictionary and found out that default is not right word,

A user while entering data could select Red, and in update.jsp, the user would want to view the data entered or changed data, so that value selected by the user which could either be red, orany color is chosen.




0
 
jimmackCommented:
Sorry Jasbir, I'm still not sure I've got it.  Keep going and we'll get there ;-)

Tell me where I go wrong:

1)  A user has made their color selection on a previous page.
2)  The user then enters the update.jsp page
3)  The server knows about the user's previous color choice.  This could be either:
3.1)  Because update.jsp was called directly from the page where the color selection was made (so it's available in the request)
or
3.2)  Because the previous color selection has been stored in a session variable (or in persistent storage)
4)  The color option list is to be displayed again in the update.jsp page
5)  The previously chosen color is to be selected (highlighted) when the page is displayed.

;-)
0
 
Jasbir21Author Commented:
1)A user has made their color selection on a previous page.-Yes
2)  The user then enters the update.jsp page-No
3)  The server knows about the user's previous color choice.  This could be either:
3.1)  Because update.jsp was called directly from the page where the color selection was made (so it's available in the request)-No
or
3.2)  Because the previous color selection has been stored in a session variable -Yes(or in persistent storage)
4)  The color option list is to be displayed again in the update.jsp page-Yes
5)  The previously chosen color is to be selected (highlighted) when the page is displayed.
-Yes

Actually, when the value is stored in a session variable, but i don't know how to put the sesion variable in the statement.

For example, for textarea i do like this:
<textarea type="text" name="skill" rows="5" cols="15"  wrap readonly>
<%=vol.getSkill()%>
</textarea>

Thanks
0
 
jimmackCommented:
As before then, but with session.getAttribute() instead of request.getParameter():

You'll need to change "colorselectionattribute" to whatever you used when you did the session.setAttribute().

You'll also need to replace the colorText and colorValue arrays with the source of the RED/red BLUE/blue text and values.

<%
String[] colorText = {"RED", "BLUE", "YELLOW"};
String[] colorValue = {"red", "blue", "yellow"};
String selectedColor = session.getAttribute("colorselectionattribute");
%>
<select name="state">
<%
for (int i = 0; i < colorText.length; i++)
{
%>
<option value='<%= colorValue[i] %>'
<%
    if (colorValue[i].equals(selectedColor))
    {
        out.print(" selected");
    }
%>
><%= colorText[i] %></option>
<%
}
%>
</select>

0
 
Jasbir21Author Commented:
Hi,
 I think i did something
C:\apache\tomcat\work\Catalina\localhost\jsp-examples\org\apache\jsp\jsp2\volunteer_jsp.java:330: incompatible types
found   : java.lang.Object
required: java.lang.String
String selectedColor = session.getAttribute("state");
                                           ^
1 error


0
 
KuldeepchaturvediCommented:
I guess you are looking at something like this....

<SELECT NAME="GTYPE">
 <OPTION VALUE="TEXT" <%if(test.equals("TEXT")){%>SELECTED=""<%}%>>TEXT</OPTION>
 <OPTION VALUE="LINK" <%if(test.equals("LINK")){%>SELECTED=""<%}%>>LINK</OPTION>
 <OPTION VALUE="MONEY" <%if(test.equals("MONEY")){%>SELECTED=""<%}%>>MONEY</OPTION>
 <OPTION VALUE="DATE" <%if(test.equals("DATE")){%>SELECTED=""<%}%>>DATE</OPTION>
 <OPTION VALUE="MONEY" <%if(test.equals("NUMBER")){%>SELECTED=""<%}%>>NUMBER</OPTION></SELECT>

Here test is the value that you want to match your options with..
It may be retrieved by using
String test=request.getParameter("GTYPE");
or for your case may be by doing
test=vol.getState();

let me know if it works
0
 
krispolsCommented:
hello Jasbir21,

your error is cause you need to cast to String.

String selectedColor = (String)session.getAttribute("state");

But  Kuldeepchaturvedias gave you the good way to do what you expect.

<select name="state">
<option value='red' <% if(session.getAttribute("state").toString.equals("red")) out.print("selected") %>>RED</option>
<option value='blue' <% if(session.getAttribute("state").toString.equals("blue")) out.print("selected") %>>BLUE</option>
</select>

regards
0
 
jimmackCommented:
Kuldeepchaturvedi and krispols, Thanks for your input but you have both limited the option list to hard coded values.  The colors provided by Jasbir are examples, not necessarily the complete list.

krispols did identify the problem, however, which was the missing cast to (String) from the session.getAttribute() call ;-)
0
 
KuldeepchaturvediCommented:
The reason it is limited was because of the question was asked looked like a limited options...
Obviously in a real time world you may have the options coming from a database in the form of a resultset...
in this case every hardcoded option can be replaced with rs.getString(1). command...:-)
<%String val =rs.getString(1);%>
<OPTION VALUE=<%=val%><%if(test.equals(val)){%>SELECTED=""<%}%>>TEXT</OPTION>
does that help..........????
0
 
Jasbir21Author Commented:
hi,
  I tried and three answers solved my problem.So, i think i split points .


Thanks and God bless
0
 
jimmackCommented:
;-) - Teamwork :-)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 6
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now