java servlet中的html表单处理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5160311/
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 09:45:16  来源:igfitidea点击:

html form processing in java servlet

javahtmlservlets

提问by code-gijoe

I'm building a java servlet to respond to some HTML form. Here is the simple test form:

我正在构建一个 java servlet 来响应一些 HTML 表单。这是简单的测试表格:

 <FORM action="http://somesite.com/prog/adduser" method="post">
    <P>
    <LABEL for="firstname">First name: </LABEL>
              <INPUT type="text" id="firstname"><BR>
    <LABEL for="lastname">Last name: </LABEL>
              <INPUT type="text" id="lastname"><BR>
    <LABEL for="email">email: </LABEL>
              <INPUT type="text" id="email"><BR>
    <INPUT type="radio" name="sex" value="Male"> Male<BR>
    <INPUT type="radio" name="sex" value="Female"> Female<BR>
    <INPUT type="submit" value="Send"> <INPUT type="reset">
    </P>
 </FORM>

On the server side I get the HttpRequest alright. But when I get the parameters like this:

在服务器端,我得到了 HttpRequest。但是当我得到这样的参数时:

@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    String firstName = req.getParameter("firstname");
    String lastName = req.getParameter("lastname");
    String sex = req.getParameter("sex");
    String email = req.getParameter("email");

}

Only the "sex" is ok. I've been at this for hours without understanding why "sex" is different from the rest. All other parameters are null. Ok it's the only "radio" type but is there a special way to get the parameters for the others?

只有“性”是可以的。我已经研究了几个小时,但不明白为什么“性”与其他的不同。所有其他参数都为空。好的,这是唯一的“收音机”类型,但有没有一种特殊的方法来获取其他人的参数?

Thank you!

谢谢!

回答by Raceimaztion

In an HTML Form it is important to give your Input values the Name attribute, the ID attribute only helps Javascript in the page find your Input elements better.

在 HTML 表单中,重要的是为您的 Input 值提供 Name 属性,ID 属性只会帮助页面中的 Javascript 更好地找到您的 Input 元素。

I noticed that you missed Name attributes for your fist few Input elements.

我注意到你错过了你的前几个 Input 元素的 Name 属性。

回答by Kurt Kaylor

You need to add the nameattribute with the rest of the input tags, like you did with the sexinput tag:

您需要将name属性与输入标签的其余部分一起添加,就像您对sex输入标签所做的一样:

<FORM action="http://somesite.com/prog/adduser" method="post">
<P>
<LABEL for="firstname">First name: </LABEL>
          <INPUT type="text" id="firstname" name="firstname"><BR>
<LABEL for="lastname">Last name: </LABEL>
          <INPUT type="text" id="lastname" name="lastname"><BR>
<LABEL for="email">email: </LABEL>
          <INPUT type="text" id="email" name="email"><BR>
<INPUT type="radio" name="sex" value="Male"> Male<BR>
<INPUT type="radio" name="sex" value="Female"> Female<BR>
<INPUT type="submit" value="Send"> <INPUT type="reset">
</P>