Assigning a jsp variable a value based on HTML selection list
<%String m="1"; %>
In my JSP file
<select name ="test">
   <option value="2"> 2 </option>
   <option value="3"> 3 </option>
   <option value="4"> 4 </option>开发者_StackOverflow中文版
</select>
Based on what is chosen on selection list I want to assign value to the variable m. What is the best approach to do this. Thanks.
Put it in a HTML <form> and add a submit button.
<form>
    <select name="test">
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="3">3</option> <!-- Why 2 same options? -->
    </select>
    <input type="submit" />
</form>
This way the variable will be sent as HTTP request parameter with the name of the input element as parameter name. You can then get it as follows:
String m = request.getParameter("test");
// ...
Note that postprocessing a form submit is normally to be done in a servlet, not a JSP.
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论