Registration Form in Servlet

Registration Form in Servlet

In this article, we will see how to develop a Registration Form in Servlet. To develop a registration form we will need to connect our servlet application with the database. Here we are using the MySQL database.

Registration Form in Servlet

Create a table “login” in your database as shown below:

CREATE TABLE LOGIN
(
   Name VARCHAR2(100),
   Email VARCHAR2(100),
   Password VARCHAR2(100)
)

In this example, we have created three files:

  1. index1.html
  2. RegisterServlet.java
  3. Welcome1.java

In index1.html, we have to get input from the user using text fields and a combo box. The information entered by the user has forwarded to RegisterServlet.java servlet which is responsible to store the data into the database. RegisterServlet.java servlet receives all the data entered by the user and stores it into the database. Here, we are performing the database logic. But you may separate it, which will be better for the web application.

index1.html
<html>
<body>
 <form action="RegisterServlet" method="post"> Name:
  <input type="text" name="name" />
  <br />
  <br /> Email Id:
  <input type="text" name="email" />
  <br />
  <br /> Password:
  <input type="password" name="password" />
  <br />
  <br />
  <br />
  <br />
  <input type="submit" value="register" formaction="RegisterServlet">
  <input type="submit" value="Login" formaction="LoginServlet"> </form>
</body>
</html>
RegisterServlet.java
package com.servlet;
import java.io.*;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

@WebServlet ("/RegisterServlet")
public class RegisterServlet extends HttpServlet
{
    private static final long serialVersionUID = 1L;
    public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        response.setContentType ("text/html");
        PrintWriter out = response.getWriter ();

        String name = request.getParameter ("name");
        String email = request.getParameter ("email");
        String password = request.getParameter ("password");
        
        HttpSession session = request.getSession ();
        session.setAttribute ("email", email);

        try
        {
            Class.forName ("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection ("jdbc:mysql://localhost:3306/userdb", "root", "876745");
            PreparedStatement ps = con.prepareStatement ("insert into login values(?,?,?)");
            ps.setString (1, name);
            ps.setString (2, email);
            ps.setString (3, password);

            int i = ps.executeUpdate ();
            if (i > 0)
             out.print ("You are successfully registered...");
            request.getRequestDispatcher ("Welcome1").include (request, response);

        }
        catch (Exception e2)
        {
            System.out.println (e2);
        }
        out.close ();
    }
}
Welcome1.java
package com.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

@WebServlet("/Welcome1")
public class Welcome1 extends HttpServlet
{
    private static final long serialVersionUID = 1L;
    protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        response.setContentType ("text/html");
        PrintWriter out = response.getWriter ();
        String user = request.getParameter ("name");
        out.println ("</br></br>");
        out.println ("Welcome " + user);
        out.println ("</br></br>");
        out.println ("<a >Click here to Login</a>");
    }
}
Output

Run your index1.html file, you will get the following output:

Registration Form in the Servlet application

Enter your details and click on the register button and you will get the below output.

how to develop a Registration Form in Servlet

In the next article, I am going to discuss How to Fetch data from a database using Servlet. Here, in this article, we develop a Registration Form in the Servlet application which will register the user into the database table and I hope you enjoy this article.

Leave a Reply

Your email address will not be published. Required fields are marked *