Registration Form Using Servlet in Java

Introduction

This article explains how to create a registration form in a Servlet. The Netbeans IDE is used for this example.

Registration Form in Servlet

This article explains how to create a simple registration form in a Servlet. In this registration form the following tools are used:

  • Oracle 10g database
  • Tomcat server
  • NetBeans IDE

We need to create the following files:

  1. userlogin table
  2. index.html file
  3. SignupServlet.java file
  4. web.xml file

1. userlogin table

We need a table to store user information in the database. In this "registration form" we create a table named "userlogin".

Syntax for table:

create table userlogin(name varchar2(4000), password varchar2(4000), emailid varchar2(4000), country varchar2(4000));

Or use the following procedure.

Step 1

Open the database home page and login to your account as in the following:

fig-1.jpg

Step 2

Now click on "Object Browser" and choose "Create" -> "Table" as in the following:.

fig-2.jpg

Step 3

Now type the following details of your table (as table name and their column names) as in the following:

Fig-3.jpg

Step 4

Now click on "Next" until you reach the create page, then your table is created as in the following:

Fig-4.jpg

Now to create the other files:

  • index.html
  • SignUpServlet.java
  • web.xml

We need to use the following procedure for this.

Step 5

Open the NetBeans IDE.

Fig-5.jpg

Step 6

Choose "Java web" -> "Web project" as in the following:

Fig-6.jpg

Step 7

Now type your project name as "RegistrationForm" as in the following:

Fig-7.jpg

Step 8

Click on "Next" and choose your server wizard; I selected Tomcat7 as in the following:.

Fig-8.jpg

Step 9

Now delete your default "index.jsp" file and create a new "index.html" file and write the following code there.

index.html

<!DOCTYPE html>

<html>

    <head>

        <title>User Registration Form</title>

    </head>

    <body bgcolor="pink">

    <center><h1>Registration Form</h1></center><br>

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

        <table>

            <tr>

                <td>

                    Name:

                </td>

                <td>

                    <input type="text" name="uname">          

                </td>

            </tr>

            <td>

                Create Password:

            </td>

            <td>

                <input type="password" name="upass">

            </td>

            <tr>

                <td>

                    Confirm Password:

                </td>

                <td>

                    <input type="password" name="upass">

                </td>

            </tr>

            <tr>

                <td>

                    Email-Id:               

                </td>

                <td>

                    <input type="text" name="uemailId">

                </td>           

            </tr>

            <tr>

                <td>

                    Country:

                </td>

                <td>

                    <select name="ucountry">

                        <option>India </option>

                        <option>Pakistan</option>

                        <option>Bangladesh</option>

                        <option>japan</option>

                        <option>Canada</option>

                    </select>

                </td>

            </tr>

            <tr>

                <td>

                    <input type="submit" value="Login">

                </td>

                <td>

                    <input type="reset" value="reset">

                </td>

            </tr>

        </table>

    </form>

</body>

</html>

Step 10

Now create a servlet file with the name "SignUpServlet" and provide the following code for it.

SignUpServlet.java

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.sql.*;

public class SignUpServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        response.setContentType("text/html");

        PrintWriter out = response.getWriter();

        String n = request.getParameter("uname");

        String p = request.getParameter("upass");

        String e = request.getParameter("uemailId");

        String c = request.getParameter("ucountry");

        try {

            Class.forName("oracle.jdbc.driver.OracleDriver");

            Connection con = DriverManager.getConnection("jdbc:oracle:thin:@mcndesktop07:1521:xe", "sandeep", "welcome");

            PreparedStatement stmt = con.prepareStatement("insert into userlogin values(?,?,?,?)");

            stmt.setString(1, n);

            stmt.setString(2, p);

            stmt.setString(3, e);

            stmt.setString(4, c);

            int i = stmt.executeUpdate();

            if (i > 0) {

                out.println("You are successfully registered.....");

            }

        } catch (Exception ey) {

            System.out.println(ey);

        }

        out.close();

    }

}

Step 11

Now ensure that the code of your web.xml file is the same as:

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <servlet>

        <servlet-name>SignUpServlet</servlet-name>

        <servlet-class>SignUpServlet</servlet-class>

    </servlet>

    <servlet-mapping>

        <servlet-name>SignUpServlet</servlet-name>

        <url-pattern>/SignUpServlet</url-pattern>

    </servlet-mapping>       

</web-app>

Step 12

Now your project is ready.

Right-click on the project menu then select "Run" as in the following:

Fig-9.jpg

The following output is generated.

Fig-10.jpg

Now fill in the detail as shown.

Fig-11.jpg

Click on "Reset" if you think you would pass some incorrect data. The demo of the reset is shown below. Since here I typed the wrong emailid, now I click on the Reset button.

Fig-12.jpg

Fill in the correct detail. I filled in the following details.

Fig-13.jpg

Now click on "Finish"; the "SignUpServlet" is showing the following message.

Fig-14.jpg

Note

Now your data is saved in the database you created at the beginning of this article.

To see the data in the database,

open the database home page and

go to "Object Browser" -> "View" -> "Table". Select your table as "userlogin". Now click on "data" that is just shown up over the table.

Fig-15.jpg


Similar Articles