How to Create a Login Page Using Java Servlet and JDBC – Step-by-Step Guide for Beginners

How to Create a Login Page Using Java Servlet and JDBC – Step-by-Step Guide for Beginners

How to Create a Login Page Using Java Servlet and JDBC – Step-by-Step Guide for Beginners

In this blog, we’ll walk you through the entire process of creating a simple yet powerful login page using Java Servlet and JDBC. This practical is part of the BSc IT Semester 5 curriculum and corresponds to Practical 1B: “Create a servlet for a login page. If the username and password are correct, it should display a message ‘Hello’, else show ‘Login Failed’.”

This project is a foundational step for any student looking to understand Java web application development. It combines the use of HTML forms, Java servlets, JDBC connectivity, and a MySQL database. The entire process not only strengthens your understanding of Java EE but also introduces you to real-world web development practices.

🎯 Objective:

The aim is to create a login page where the user can enter a username and password. The credentials are verified against the database using JDBC. If matched, a welcome message is displayed. If not, an error message like "Login failed" is shown.

📋 Step-by-Step Explanation

Step 1: Set Up the Environment

  • Install NetBeans IDE 8.2 or above
  • Install XAMPP or any MySQL server
  • Make sure Apache Tomcat or GlassFish Server is configured properly

Step 2: Create a Dynamic Web Project

  • Open NetBeans and go to File → New Project → Java Web → Web Application
  • Name the project (e.g., LoginApp)
  • Select your server (GlassFish or Tomcat) and Java EE version

Step 3: Design the Login Form

Inside your Web Pages or HTML folder, create a simple login form with fields for username and password. This form will submit data to the Java servlet.

Step 4: Configure the Database

  • Open phpMyAdmin or MySQL CLI
  • Create a database (e.g., loginDB)
  • Add a table (e.g., users) with columns: username and password
  • Insert a few sample records for testing

Step 5: Create the Servlet

In the src/java folder, create a new Java class that extends HttpServlet. This servlet will handle the logic for checking login credentials against the database using JDBC.

🌐 HTML



<html><head><title>Login Form</title></head>
<form action="LoginServlet" >
Enter User ID <input type="text" name="txtId"><br>
Enter Password <input type="password" name="txtPass"><br>
<input type="reset"><input type="submit" value=" Click to Login " >
</form></html>

🌐 JAVA



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

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

    String uname = request.getParameter("txtId");
    String upass = request.getParameter("txtPass");

    if(uname.equals("admin") && upass.equals("12345")) {
      out.println("<body bgcolor=blue >");
      out.println("<h1> Welcome !!! " + uname + "</h1>");
    } else {
      out.println("<body bgcolor=red >");
      out.println("<h1> Login Fail !!! </h1>");
    }
    out.println("</body></html>");
  }
}

🔍 Output Preview


Step 6: Establish JDBC Connection

Use the JDBC driver to connect the servlet to your MySQL database. This will allow the servlet to validate whether the entered username and password exist in the database.

Step 7: Test the Application

  • Run the application on your configured server
  • Open the login form in your browser
  • Enter valid credentials – you’ll see a welcome message
  • Enter invalid credentials – you’ll see “Login Failed”

🚀 What You Learn from This Practical

  • How to design HTML forms and link them with servlets
  • How to use JDBC to connect to a database in Java
  • How servlet request handling works in Java EE
  • Real-world login validation technique

💡 Why This Practical Matters

Most modern websites and applications rely on user login systems. Understanding how to build one from scratch using Java Servlet and JDBC gives you a competitive edge. You’ll gain skills in Java EE web development, backend validation, session management, and more.

🔍 SEO Keywords:

Java Servlet Login Page, JDBC Login Authentication, Java EE Web Project, Java Web Application with Login Form, BSc IT Practical Java, Login Page using Servlet and MySQL

✅ Conclusion

This practical is a must-do for anyone pursuing Java backend development. It combines front-end HTML, backend Servlet logic, and database validation using JDBC. By completing this step-by-step project, you not only fulfill your academic requirements but also build a solid foundation for future Java projects, including user registration, session tracking, and admin panel creation.

Next Step: Try enhancing this project by adding session management, logout functionality, or even a registration page!

🔗