This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes JSP and the fly likes JSP Drop Down List Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP Drop Down List" Watch "JSP Drop Down List" New topic

JSP Drop Down List

Michael James Payne

Joined: Jan 29, 2004
Posts: 9
How do I mainitain the selected value from the dropdown list so when the jsp is redisplayed, the selected values are maintained (are shown) and not lost?

Michael J. Payne
Fusion Alliance
Indianapolis, Indiana

Michael J. Payne
Stefan Evans

Joined: Jul 06, 2005
Posts: 1580
You have to generate appropriate HTML to indicate which records in the list are selected.
Basically for whichever option is selected, you have to have <option value="42" selected="true">

Frameworks like struts/JSF will often take care of this for you, otherwise you do it manually, with an if statement in the middle of your rendering the list.
Another approach is to render the list as usual, and then execute a javascript command onload which sets the value. It sometimes looks a bit neater in code ;-)
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63873

One caveat:


is probably a poor way to express this because it would lead one to believe that saying selected="false" would have the opposite effect.

In XHTML, if anything is supplied as a value to the selected attribute -- be it selected="true" or selected="false" or even selected="sdhsjhdsh" -- the option will be selected.

This is true of all attributes that have no value in HTML but must have a value in XHTML.

The standard means of expressing these is to repeat the name of the attribute. E.g. selected="selected".
[ October 19, 2006: Message edited by: Bear Bibeault ]

[Asking smart questions] [About Bear] [Books by Bear]
toukhir mujawar
Ranch Hand

Joined: Oct 11, 2006
Posts: 70

just try this one...

<option value="1" selected>

this is sufficient... need not write select= "true"/false/anything else...
reddy yeruva

Joined: Oct 18, 2006
Posts: 18
ok if you want the selected value in the page after refresh means again you are sending it to the same page yes..


place in javascript as



Y.Tirupati Reddy<br />
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63873

Originally posted by toukhir mujawar:
need not write select= "true"/false/anything else...

Depends upon whether you are using HTML or XHTML. In the latter, the value is required.
I agree. Here's the link:
subject: JSP Drop Down List
It's not a secret anymore!