Actually, the example I posted was not typical of the select building code in my jsp. The initialization of the String select5 = "" should have been inside the for loop as it is in all my other code fragments.

That aside, the problem persists.

The addition of } else { to the if that checks the request parameter does not solve the problem. The only time the <option> tags get written is inside the for loop, and at the top of the for loop the string is initialized to "" again. Once the for loop ends, no <option> tags will be written, and hence the value of select5 will be irrelevant as it will not be used for anything.

So this is still a strange inconsistency. I'm going to try the code on a different JSP engine to see what happens.

As a sanity check, I also tried using the else, but my logic above is correct and it had no effect.

Has anyone on the list experienced any problems like this before with JSP?

June Low wrote:

hi,     I think the problem is with the "select5", look the follwoing code carefully and u'llknow y: <SELECT NAME="sortby">
<%
  String select5 = "";
  System.out.println(request.getParameter("sortby"));
  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> 
===
Regards,
Kiam Imm,  June :)

----- Original Message -----
Sent: Thursday, November 04, 1999 2:49 PM
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

Reply via email to