简体   繁体   中英

Sending form radio values to Java servlet, returns null

When I want to send a radio value from a form on a HTML page to a Java Servlet, this always seems to be a null value, instead of "1" or "2" or...

Here is the HTML code I have:

<form method="post" action="/testWeb/animalsurvey" target="_blank">

<p>What is your favorite pet?</p>

<p><input type="radio" name="animal" value="1" />Dog<br />
<input type="radio" name="animal" value="2" />Cat<br />
<input type="radio" name="animal" value="3" />Bird<br />
<input type="radio" name="animal" value="4" />Snake<br />
<input type="radio" name="animal" value="5" checked="checked" />None</p>

<p><input type="submit" value="Submit" /></p>

This is the Java Servlet code:

protected void doPost(HttpServletRequest request,
                      HttpServletResponse response) throws ServletException, IOException {

    PrintWriter out = response.getWriter();

    String value = request.getParameter("name");

    out.println(value);

}

Web.xml code, which show the servlet mapping:

<servlet>
    <servlet-name>animalsurvey</servlet-name>
    <description>A survey servlet.</description>
    <servlet-class>be.SurveyServlet</servlet-class>
</servlet>


<servlet-mapping>
    <servlet-name>animalsurvey</servlet-name>
    <url-pattern>/animalsurvey</url-pattern>
</servlet-mapping>

I don't know if I'm blind to see it here, but what am I doing wrong?

Looks like it is the parameter name:

Please try:

 String value = request.getParameter("animal");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM