java 单选按钮的 HttpServletRequest getParameter

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5267946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:19:58  来源:igfitidea点击:

HttpServletRequest getParameter of Radio Buttons

javaservletsradio-button

提问by Johnrad

I want to have the parameter value of radio button.

我想要单选按钮的参数值。

<input type="radio" name="lang" value="Official"/>Official Only
<br/>
<input type="radio" name="lang" value="all"/>All

How do I check in java to see which one is selected?

我如何检查java以查看选择了哪一个?

回答by BalusC

Use HttpServletRequest#getParameter()with the name of the input field as parameter name.

使用HttpServletRequest#getParameter()与输入字段作为参数名的名称。

String lang = request.getParameter("lang"); // Can be null, "Official" or "all"

The value of the input field will become the parameter value.

输入字段的值将成为参数值。

if ("Official".equals(lang)) {
    // Official selected
} else if ("all".equals(lang)) {
    // all selected
}