How to Retrieve Data from an HTML Form in a Java Servlet

From WikiHTP

We will now see that a servlet can receive data from an HTML form. We will implement an HTML form that requests the input of a username and password and then retrieves the two data in a servlet and shows them on another page generated by the servlet.

Create a Project[edit]

  • First we will create a project called formToServlet.
  • To create the HTML file with the form, right-click on the WebContent->New->HTML File.
  • Specify the filename as form.html and then click Finish.

Create HTML Page[edit]

We codify the HTML page with the web form that requests the input of the username and its password.

<! DOCTYPE html PUBLIC "- // W3C // DTD HTML 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
      <head>
        <meta http-equiv = "Content-Type" content = "text / html; charset = utf-8">
        <title> Insert title here </ title>
    </head>
    <body>
      <form method = "post" action = "UserRecovery">
        Enter username:
        <input type = "text" name = "user" size = "20"> <br>
        Enter password:
        <input type = "password" name = "key" size = "20"> <br>
        <input type = "submit" value = "confirm">
      </form>
    </body>
</html>

The most important thing when we create the web form which will specify the action property of the brand form with the name of the servlet that will retrieve the form data.

<form method = "post" action = "UserRecovery">

Servlet Code[edit]

Now we create the servlet that as we see it should be named as "UserRecovery". The servlet source code is the following:

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/ **
 * Servlet implementation class UserRecovery
 * /
@WebServlet ("/ UserRecovery")
public class UserRecovery extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    / **
     * @see HttpServlet # HttpServlet ()
     * /
    public RecuperacionUsuario () {
        super();
        // TODO Auto-generated constructor stub
    }

    / **
     * @see HttpServlet # doGet (HttpServletRequest request, HttpServletResponse response)
     * /
    protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    / **
     * @see HttpServlet # doPost (HttpServletRequest request, HttpServletResponse response)
     * /
    protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
         PrintWriter out = response.getWriter ();

         out.println ("<html>");
         out.println ("<head> </ head>");         
         out.println ("<body>");

         out.println ("User:");
         String usu = request.getParameter ("user");
         out.println (usu);
         out.println ("<br>");
         out.println ("Password:");         
         String cla = request.getParameter ("key");
         out.println (cla);
         
         out.println ("</ body>");
         out.println ("</ html>");           
    }
}

As we can see in the source code of the UserRecovery class we must implement all the code in the doPost method, since this is executed when you have an HTML form and the post value was specified in the HTML in the method property.

To retrieve the text and password controls of the HTML form, the request object of the HttpServletRequest class has a method called getParamenter that tells you the name of the control to recover:

String usu = request.getParameter ("user");

After recovering it, we proceed to show it inside the generated page:

out.println (usu);

To test our application we must press the right button on the form.html and select "Run As" -> "Run on Server", then the result of the execution in the browser.

About This Tutorial

This page was last edited on 18 February 2019, at 20:26.