简体   繁体   中英

Servlet request.getParameter() always returning “null”

I have read through all related questions, trying every accepted answer and I still am finding no luck.

I have a website running on tomcat, with a subpage /Demo/ which has four text fields and a Submit button. The submit button looks as follows

 <form method="post" action="DemoServlet">
                 <input type="hidden" name="form_action" value="write" />
                 <table>
                    <tr>
                       <td>
                          First Name:
                       </td>
                       <td>
                          <input type="text" 
                             name="firstname" />
                       </td>
                    </tr>
                    <tr>
                       <td>
                          Last Name:
                       </td>
                       <td>
                          <input type="text" 
                             name="lastname" id = "lastname" />
                       </td>
                    </tr>
                    <tr>
                       <td>
                          Email:
                       </td>
                       <td>
                          <input type="text" 
                             name="recipient" />
                       </td>
                    </tr>
                    <tr>
                       <td>
                          Phone1:
                       </td>
                       <td>
                          <input type="text" 
                             name="phone" />
                       </td>
                    </tr>
                    <tr>
                       <td>
                          <input type=button onClick="location.href='../demo-servlet'" value='Submit'/>
                       </td>
                       <td>
                       </td>
                 </table>
              </form>

This /demo-servlet is specified in web.xml as follows

 <servlet>
    <servlet-name>DemoServlet</servlet-name>
    <servlet-class>PACKAGENAME.DemoServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>DemoServlet</servlet-name>
    <url-pattern>/demo-servlet</url-pattern>
</servlet-mapping>

This servlet looks as follows

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

    // Retrieve First Name from /Demo/ text field
    firstName = request.getParameter("firstname");

    // Retrieve Last Name from /Demo/ text field
    lastName = request.getParameter("lastname");
    /* MORE CODE HERE */
    request.getRequestDispatcher("/WEB-INF/confirmation.jsp").forward(request, response);
}

Which then forwards to my confirmation.jp file, showing that the process has succeeded.

My problem is, the variables and both return the value "null" after the request.getParameter() function is called.

Anyone have a clue why this is happening?

The method on your form tag is post but you have implemented doGet in your servlet. Also the action on your form tag is DemoServlet but should be something like ../demo-servlet You probably don't need onClick at all.

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