Hi there all,

Apologies if something similar has been posted before but I tried a search and couldn't find anything.


I have a combobox with 2 options "OPEN" and "CLOSED".
Tied to this combo box I have a scriptlet which I want to select the appropriate value of the combo box depending on a database value.
I'm currently trying to get it working something like this:

if (incidents.getINC_STATUS() = "OPEN")
<option value="OPEN" Selected="true">
else
<option value="CLOSED" Selected="true">;


I'm still pretty new to java and JSP. I know it won't run the html option value's like that within a scriptlet but I'm not sure how to get around this.

If anyone has any ideas or can see another method, it would be most appreciated.

Kind regards,

Rob.

well, for one thing you're using an assignment operation where you're probably intending to use a comparison operation.
And that comparison operation would also be incorrect as it's not the way to compare Strings in Java.

<c:choose>
  <c:when test="incidents.INC_STATUS == 'open'">
    <option value="OPEN" selected="selected"/>
    <option value="CLOSED"/>
  </c:when>
  <c:otherwise>
    <option value="OPEN"/>
    <option value="CLOSED" selected="selected"/>
  </c:otherwise>
</c:choose>

would be a proper way to do it in JSP (though there are some variations on the theme, like determining whether to set an option to true in that option, which is the best way if the number of options gets to be more than two or three).

Thanks jwenting for your reply. However I don't think i'm managing to implement your code corrctly. I've tried numerous times and methods but <c:choose> is always reported as "element choose not expected" so when run, currently the combo box presents 4 blank options. I've tried naming them too e.g.

<option value="OPEN" selected="selected">OPEN</option>

but again I get presented by 4 options: OPEN, CLOSE, OPEN, CLOSE
This is how I have incorporated it currently:

<td>
            <select size="1" name="INC_STATUS">
              <c:choose>
                <c:when test="incidents.INC_STATUS == 'OPEN'">
                    <option value="OPEN" selected="selected">
                    <option value="CLOSED"/>
                </c:when>
                <c:otherwise>
                    <option value="OPEN"/>
                    <option value="CLOSED" selected="selected">
                </c:otherwise>
              </c:choose>
            </select>
          </td>

If you could shed any light on where i'm making mistakes that would be great. As I said before I'm very new to this so I could be making some very stupid mistake!

Thanks,
Rob.

You will need to add the correct declarations to the top of your JSP to enable JSTL.
That will have to be accompanied by a correctly versioned web.xml in order to get the servlet container to initialise support for JSTL correctly.

Thanks for that jwenting, I have imported the JSTL library and now it appears to be working fine (except it doesnt seem to like the comparison- something i need to look into with my db i think)

Had to look around as i wasn't sure how to import it but as I say managed to do it in the end.

Thanks for your help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.