Hi Scott,

It's because you are only resetting your select5 variable before the loop.
That means once you have found a selected option within your loop, every
option after that will be set to " SELECTED", regardless.  Either reset your
select5 variable each time through the loop, or add an else block to your
if.  E.G.

<SELECT NAME="sortby">
<%
  String select5 = "";
  String[] sort = {"ASC", "DESC"};
  for (int i = 0; i < 2; i++) {
    String sb = (String) sort[i];
    if (sb.equals(request.getParameter("sortby")))
    {
      select5 = " SELECTED";
    }

    else
    {
      select5 = "";
    }
    %>
      <OPTION VALUE="<%=sb%>"<%=select5%>><%=sb%></OPTION>
    <%
  }
%>
</SELECT>


-----Original Message-----
From: Scott Douglass [mailto:[EMAIL PROTECTED]]
Sent: Friday, November 05, 1999 9:50 AM
To: [EMAIL PROTECTED]
Subject: weird bug or side effect in jsp interpreter?


I've got a form with a lot of elements in it. The elements of this form have
certain attributes specified based on parameters in the request object.
For example (I hope people won't object to HTML in mail...):
<SELECT NAME="sortby">
<%
  String select5 = "";
  String[] sort = {"ASC", "DESC"};
  for (int i = 0; i < 2; i++) {
    String sb = (String) sort[i];
    if (sb.equals(request.getParameter("sortby")))
    {
      select5 = " SELECTED";
    }
    %>
      <OPTION VALUE="<%=sb%>"<%=select5%>><%=sb%></OPTION>
    <%
  }
%>
</SELECT>
The first time the jsp is run (there's no data in the request object), it
acts as expected. Neither of the <OPTION> have the SELECTED attribute. The
second time this page is loaded (by submitting the form) there is now
request data, and the SELECTED attribute is correctly applied to the
selected option. However, on the 2nd time the form is submitted, with the
other option selected, BOTH <OPTIONS> end up with the SELECTED attribute!
How can this be? The code clearly says that only the option that was stored
in the request object should have the selected attribute. I can't figure
this out.
All of my <SELECT> widgets on this form (there's five of them) have this
problem. In every case, no matter how many <OPTION> there are, I always end
up with TWO of them having the SELECTED attribute.
I'm using Resin 1.1b3 as my Servlet/JSP engine, Apache 1.3.9 as my httpd,
Solaris 2.7 on UltraSPARC as my os/server. I know that the parameters of the
request object have the correct values, despite what shows up in the HTML
the client sees, as I'm printing out the SQL query I'm building on the page
each time it loads.
Here's the source, as seen from the client (IE5):
1. First time the page is loaded
<SELECT NAME="sortby">
  <OPTION VALUE="ASC">ASC</OPTION>
  <OPTION VALUE="DESC">DESC</OPTION>
</SELECT>
2. Second time through after submitting the form with DESC selected
<SELECT NAME="sortby">
  <OPTION VALUE="ASC">ASC</OPTION>
  <OPTION VALUE="DESC" SELECTED>DESC</OPTION>
 </SELECT>
3. Third time, submitting the form but selecting the ASC <OPTION>
<SELECT NAME="sortby">
  <OPTION VALUE="ASC" SELECTED>ASC</OPTION>
  <OPTION VALUE="DESC" SELECTED>DESC</OPTION>
</SELECT>
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST". FAQs on JSP can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html

Reply via email to