User Registration Using Servlet

User Registration Using Servlet

BSc IT Semester 5 – Practical 1C: Registration Servlet using JDBC

This blog explains how to create a Java Servlet that accepts registration details and stores them in a MySQL database using JDBC. This is based on Practical 1C from BSc IT Semester 5 – Database Practical List.

🎯 Objective:

Create a Registration Servlet in Java using JDBC. Accept Username, Password, Email, and Country from the user and store the data in a database.

📋 Step-by-Step Guide:

🔧 Step 1: Create the Database and Table in MySQL

Use XAMPP or any MySQL environment and run the following SQL code:


CREATE DATABASE registrationDB;
USE registrationDB;

CREATE TABLE users (
  id INT AUTO_INCREMENT PRIMARY KEY,
  username VARCHAR(100),
  password VARCHAR(100),
  email VARCHAR(100),
  country VARCHAR(100)
);
    

📽️ Step-by-Step Process (As Shown in the Video)

1️⃣ Create a Web Application Project

  • Open NetBeans IDE 8.2
  • Go to: File → New Project
  • Choose: Java Web → Web Application
  • Name the project (e.g., firstapp)
  • Select GlassFish Server and Java EE 7 Web

2️⃣ Design the Frontend (index.html)

Create the following form using HTML:

🌐 HTML



<html>
<head><title>Registration Page</title></head>
<body>
  <form action="RegisterServlet" method="get">
    <h1>Welcome to Registration Page</h1>
    Enter User Name: <input type="text" name="txtUid"><br>
    Enter Password: <input type="password" name="txtPass"><br>
    Enter Email: <input type="text" name="txtEmail"><br>
    Enter Country: <input type="text" name="txtCon"><br>
    <input type="reset">
    <input type="submit" value="REGISTER">
  </form>
</body>
</html>

🌐 JAVA



package mypack;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class RegisterServlet extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    String id = request.getParameter("txtUid");
    String ps = request.getParameter("txtPass");
    String em = request.getParameter("txtEmail");
    String co = request.getParameter("txtCon");

    try {
      Class.forName("com.mysql.jdbc.Driver");
      Connection con = DriverManager.getConnection(
        "jdbc:mysql://localhost:3306/logindb?useSSL=false", "root", "Ir@m2022");

      PreparedStatement pst = con.prepareStatement("INSERT INTO user VALUES (?, ?, ?, ?)");
      pst.setString(1, id);
      pst.setString(2, ps);
      pst.setString(3, em);
      pst.setString(4, co);

      int row = pst.executeUpdate();
      out.println("<h1>" + row + " Inserted Succesfullyyyyy");
    } catch (Exception e) {
      out.println(e);
    }
  }
}

🔍 Output Preview


🚀 Step 5: Run and Test

  • Compile your servlet and deploy it in Tomcat or GlassFish
  • Open your register.html in the browser
  • Submit the form and check your database for saved values

✅ Output:

You will see the message: "You are successfully registered!" after successful form submission and database entry.

📝 Conclusion:

This practical teaches you how to perform database connectivity using JDBC and Servlet — a fundamental concept in Java Web development. Be sure to start your MySQL service and check that all servlet mappings are correct!

🔗