Search code examples
javahtmljspservletsscriptlet

How can I select a value dinamically in JSP


I want to select a value in a select tag using scriptlet but it doesn't work. How can I achieve this?

Thanks in advance.

<%int number=2;%>
<br><br>Matricula: 

<select id="mat" name="mat" >

<option<%if(number==1){%>selected<%}%> >
01</option>
<option<%if(number==2){%>selected<%}%> >
02</option>
<option<%if(number==3){%>selected<%}%> >
03</option>
<option<%if(number==4){%>selected<%}%> >
04</option>

</select>

Solution

  • You need to add space between option and selected

    <%int number=2;%>
    <br><br>Matricula: 
    
    <select id="mat" name="mat" >
    
    <option <%if(number==1){%>selected<%}%> >
    01</option>
    <option <%if(number==2){%>selected<%}%> >
    02</option>
    <option <%if(number==3){%>selected<%}%> >
    03</option>
    <option <%if(number==4){%>selected<%}%> >
    04</option>
    
    </select>