Search code examples
javajspjsp-tags

Using jsp to detect the value of a html select menu


I am new to a Java environment and JSP. My question is, is is possible to detect the value of a select dropdown option and create a small if statement depending on this value?

For example:

<select id="my_select">
    <option value="-1">Option 1</option>
    <option value="0">Option 2</option>
</select>

<c:if test="${id eq -1}">
    Show this
</c:if>

<c:if test="${id gt -1}>
    Show this
</c:if>

Thanks guys!


Solution

  • No, you have to first realize the lifecycle of the JSP. The output is formed on the server and sent to the client. Then the client performs some actions and possibly submits a form back to the server.

    In your case you have two options:

    • use javascript
    • submit the form holding the dropdown, and depending on ${param.id} show one or the other