开发者

Assigning a jsp variable a value based on HTML selection list

开发者 https://www.devze.com 2023-03-27 01:17 出处:网络
<%String m=\"1\"; %> In my JSP file <select name =\"test\"> <option value=\"2\"> 2 </option>
<%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.

0

精彩评论

暂无评论...
验证码 换一张
取 消