在java servlet中处理html表单 [英] html form processing in java servlet

查看:105
本文介绍了在java servlet中处理html表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个Java servlet来响应某个HTML表单。以下是简单的测试表单:

 < FORM action =http://somesite.com/prog/addusermethod = POST > 
< P>
< LABEL for =firstname>名字:< / LABEL>
< INPUT type =textid =firstname>< BR>
< LABEL for =lastname>姓氏:< / LABEL>
< INPUT type =textid =lastname>< BR>
< LABEL for =email>电子邮件:< / LABEL>
< INPUT type =textid =email>< BR>
< INPUT type =radioname =sexvalue =Male>男性< BR>
< INPUT type =radioname =sexvalue =Female>女性< BR>
< INPUT type =submitvalue =发送> < INPUT type =reset>
< / P>
< / FORM>

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

$ $ $ $ $ $ $ $ $ $ $公共无效doPost(HttpServletRequest req,HttpServletResponse响应)
throws ServletException,IOException {
String firstName = req.getParameter(firstname);
String lastName = req.getParameter(lastname);
String sex = req.getParameter(sex);
String email = req.getParameter(email);

}

只有性才可以。我不知道为什么性别与其他人不同,我一直在这里呆了几个小时。所有其他参数都是空的。确定它是唯一的广播类型,但有没有一种特殊的方式来获取其他参数?

谢谢!

我注意到您错过了您的第一个输入元素的名称属性。


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

 <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:

@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!

解决方案

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.

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

这篇关于在java servlet中处理html表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆