Servlet request.getParameter() always returning "null"

19,232

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.

Share:
19,232
Trent
Author by

Trent

I’m a Developer and Maker who is interested in automation and embedded systems. I was born in the United States 🇺🇸 , growing up in Arizona 🏜 and currently living in New York City 🗽. In my free-time, I work on lots of hobby projects. Visit my GitHub profile to see what I've been working on!

Updated on June 04, 2022

Comments

  • Trent
    Trent almost 2 years

    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?

  • Trent
    Trent almost 10 years
    Changed my <form> to <form method="post" action="../demo-servlet"> and my doGet to doPost. Everything works perfectly now. Thank you very much.