Skip to content
Related Articles

Related Articles

Servlet – Registration Form

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 04 Mar, 2022

We will need to link your servlet application to a database in order to create a registration form. Here we are using MySQL database. Registration form using Html, Servlet, and MYSQL.

  1. Design registration page using Html.
  2. Create a Database and table in the MYSQL workbench.
  3. Database connectivity in java using JDBC MYSQL.

We can see the flow of the Registration form below diagram as follows: 

Firstly we take input from the user by using the index.html file after this step we send that data to the GfgRegister.java file which is a servlet file. In Servlet we have connected our Mysql database by using the MySQL jar file. If there is no error in input then data will be inserted into our database table and we will get the message of successful insertion of data.  

Implementation: 

Creating a Table in Database

create table GfgLogin
(
   name varchar(60),
   email varchar(60),
   pass varchar(100)
)

A. File: web.xml

XML




<web-app  version="3.0"
    
    <servlet>
        <servlet-name>GfgRegister</servlet-name>
        <servlet-class>GfgRegister</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>GfgRegister</servlet-name>
        <url-pattern>/GfgRegister</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>


 

 

B. File: GfgRegister.java

 

Java




import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
 
public class GfgRegister extends HttpServlet {
     
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
     
        String name = request.getParameter("name");
        String email = request.getParameter("email");
        String pass = request.getParameter("pass");
         
        try {
         
            // loading drivers for mysql
            Class.forName("com.mysql.jdbc.Driver");
             
            //creating connection with the database
            Connection con = DriverManager.getConnection
                        ("jdbc:mysql://localhost:3306/geeksforgeeks","root","root");
 
            PreparedStatement ps = con.prepareStatement
                        ("insert into gfglogin values(?,?,?)");
 
            ps.setString(1, name);
            ps.setString(2, email);
            ps.setString(3, pass);
            int i = ps.executeUpdate();
             
            if(i > 0) {
                out.println("You are successfully registered at geeksforgeeks");
            }
         
        }
        catch(Exception se) {
            se.printStackTrace();
        }
     
    }
}


 

 

C. File: index.html

 

HTML




<html>
    <head>
        <title>GeeksForGeeks Register form</title>
    </head>
    <body>
    <center>
        <form method="post" action="GfgRegister">
        Name:<input type="text" name="name" /><br/><br/>
        Email ID:<input type="text" name="email" /><br/><br/>
        Password:<input type="text" name="pass" /><br/><br/>
        <input type="submit" value="GfgRegister" /><br/>
        </form>
       </center>
    </body>
</html>


 

 

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

 

 

After you’ve entered your information and clicked the register button, you’ll see the screen below.

 

 

Data stored in the database is as shown below in below media as follows: 

 

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!