Reputation: 31
Here. i have created list box on my jsp file and i got the selected value in my servlet by using getParameter
. now i want to set the value of listbox in the same jsp file which i have previously selected when i reload the same jsp file from servlet.
jsp file
<tr><td>Operation:<select name="state" >
<option value="1">Addition</option>
<option value="2">Subtraction</option>
<option value="3">Multiplication</option>
<option value="4">Division</option>
</select></td></tr>
servlet file
operation=req.getParameter("state");
n3=Integer.parseInt(operation);
Upvotes: 0
Views: 2372
Reputation:
You need to use the select tag attribute selected
:
<select name="state">
<option value="1" ${'1' eq param.state ? 'selected' : ''}>Addition</option>
<option value="2" ${'2' eq param.state ? 'selected' : ''}>Subtraction</option>
<option value="3" ${'3' eq param.state ? 'selected' : ''}>Multiplication</option>
<option value="4" ${'4' eq param.state ? 'selected' : ''}>Division</option>
</select>
Note: make sure you are redirecting to the jsp using RequestDispatcher.forward(request, response)
${} is Expression Language(EL) provided in jsp. In EL, ${param.state}
is equivalent to request.getParameter("state")
eq
is logical operator in EL, you can even use ==
for the same.
To use with request attributes, you may try:
<option value="1" ${'1' eq requestScope.data ? 'selected' : ''}>Addition</option>
Upvotes: 1