File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Simple JSP question...Please help??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Simple JSP question...Please help???" Watch "Simple JSP question...Please help???" New topic
Author

Simple JSP question...Please help???

Moined Mogul
Ranch Hand

Joined: Jul 11, 2001
Posts: 33
I have the below piece of code that creates a drop down menu in my JSP page. This may seems like a very silly question but how would you preselect say the 4th item in the list so that on start up the JSP would open with the 4th item selected.
Thanks very much for any assistance you can give.
<%-- Populate the pulldown list of resource titles from the database. --%>
<%
String[] ResourceTitle = Functions.getTitleList();
if (ResourceTitle != null)
{
for (int i=0; i < ResourceTitle.length; i++)
{
if (ResourceTitle.equals (strSelectedResource))
{
//If the string in the strSelectedResource is
//in the pulldown list use it as the
//Preselected option to display.
%>
<OPTION selected value="<%= ResourceTitle %
>"> <%= ResourceTitle %> </OPTION>
<%
}
else
{
%>
<OPTION value="<%= ResourceTitle %>"> <%=
ResourceTitle %> </OPTION>
<%
}
}
}
else
{
%>
<OPTION> No Titles Loaded </OPTION>
<%
}
%>
</SELECT>
</td>
Jeff Sunder
Ranch Hand

Joined: Jun 26, 2001
Posts: 47
I beleive all you have to do is add'SELECTED' to the first block of your if statment:

if (ResourceTitle.equals (strSelectedResource))
{
//If the string in the strSelectedResource is
//in the pulldown list use it as the
//Preselected option to display.
%>
<OPTION selected value="<%= ResourceTitle %
>"> <%= ResourceTitle %> SELECTED</OPTION>
<%
}
else
{
%>
<OPTION value="<%= ResourceTitle %>"> <%=
ResourceTitle %> </OPTION>
<%
}
Dipti Alurkar
Greenhorn

Joined: Jul 05, 2001
Posts: 7
I think that you need to add the index of the ResourceTitle array in the 'if'.
if (ResourceTitle[i].equals (strSelectedResource))
this might help.
the 'SELECTED' word is already included in the <OPTION> tag.
Dipti
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Simple JSP question...Please help???
 
Similar Threads
select comes to default again
html:option not selected
can the drop down menu remember the last selection?
default values to set for radio / pulldown lists
ComboBox using JSP