3

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
  • 4,208
  • 5
  • 24
  • 46

1 Answers1

5

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.

Khary Mendez
  • 1,818
  • 1
  • 14
  • 18
  • Changed my
    to
    and my doGet to doPost. Everything works perfectly now. Thank you very much.
    – Trent Jul 30 '14 at 19:44