Alexey_M
Alexey_M

Reputation: 13

My servlet returns null

I wrote the following form and servlet , hoping that the servlet would return the value of the text field from the form , but it returns null instead. What should be corrected ?

<html>
<head>
<title>Simple form</title>
</head>
<body>
    <form method="post" action="theServlet">
    <input type="text" id="userName"/>
    <input type="submit" value="Post"/>
</form>
</body>
</html>



  public class theServlet extends HttpServlet {

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

        String username=request.getParameter("userName");

        response.setContentType("text/html");       
        PrintWriter writer=response.getWriter();
        writer.println("<html>");
        writer.println("userName = "+ username);    
        writer.println("</html>");

    } 
}

Upvotes: 0

Views: 76

Answers (1)

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

You should use name attribute instead of id to send parameters to the server.

<input type="text" id="userName" name="username" />

Upvotes: 2

Related Questions