Skip to content

CODEBUN

  • Home
  • Automation
    • Selenium Tutorial
    • Selenium Web Driver With Java
    • Selenium Web Driver With C#
    • Katalon studio
    • Puppeteer and Jest
  • Development
    • Java Tutorial
    • JSP Tutorial
    • Servlet Tutorial
    • Spring Tutorial
    • Hibernate Tutorial
    • Spring Boot with ReactJS
    • Angular JS
    • JS and HTML
    • C# Tutorial
    • ReactJS Tutorial
    • MERN Stack Development
    • Java web application Development tutorial
    • Database Tutorial
    • Android Tutorial
  • Projects
    • Java Projects
    • Spring Boot Projects
    • Project In Java
    • Project In Hibernate
    • Project in spring
    • Java web application Development tutorial
    • UML Diagrams
    • PHP Project
  • Programs
    • Java program
    • VB Program
    • C# Program
  • Interview Q&A
    • Placement Q&A
      • Computer || Programming
      • Maths
      • Reasoning
      • English
    • C Interview Questions and Answer
    • Data Structure Q & A
    • Operating System Interview Q&A
    • HTML and CSS Q&A
  • Downloads
Menu
  • Home
  • Automation
    • Selenium Tutorial
    • Selenium Web Driver With Java
    • Selenium Web Driver With C#
    • Katalon studio
    • Puppeteer and Jest
  • Development
    • Java Tutorial
    • JSP Tutorial
    • Servlet Tutorial
    • Spring Tutorial
    • Hibernate Tutorial
    • Spring Boot with ReactJS
    • Angular JS
    • JS and HTML
    • C# Tutorial
    • ReactJS Tutorial
    • MERN Stack Development
    • Java web application Development tutorial
    • Database Tutorial
    • Android Tutorial
  • Projects
    • Java Projects
    • Spring Boot Projects
    • Project In Java
    • Project In Hibernate
    • Project in spring
    • Java web application Development tutorial
    • UML Diagrams
    • PHP Project
  • Programs
    • Java program
    • VB Program
    • C# Program
  • Interview Q&A
    • Placement Q&A
      • Computer || Programming
      • Maths
      • Reasoning
      • English
    • C Interview Questions and Answer
    • Data Structure Q & A
    • Operating System Interview Q&A
    • HTML and CSS Q&A
  • Downloads
Search
Close this search box.
Search

Java web project login and registration with JSP and servlet with Mysql

  • Bhupendra Patidar
  • September 18, 2017
  • Java web application Development tutorial

How to login and register with JSP and Servlet using the MYSQL database. In this Java web project tutorial example, we are going to see login and Registration activity with JSP and servlet and MYSQL database.

Login and Registration with JSP and servlet with Mysql

  • Create a user table into the MySQL database.
  • Create a simple Java web project in eclipse using maven or dynamic project.
  • Create views in Jsp for the login and registration page.
  • Create a controller using a servlet that will handle requests and responses.

Here is complete video tutorial to perform login and registration with JSP and MySQL with practices that will show you, how to create java application and connect it with MSQL a database. or an idea for java database connectivity(JDBC) with MySQL so please watch the complete video first.

Tools and Technology to create a Java web project

Database:    MySQL workbench
Language : HTML or JSP,servlet
Server:         Glassfish or Tomcat

Let’s start the java database connectivity(JDBC) with MySQL.

Configure your MYSQL database

Create a new database with the name “mydb” and table name “user”. also, remember your database connection URL, User Name, and Password. that will be required at the time of database connectivity with Java.

CREATE TABLE `mydb`.`user` (
`uid` INT NOT NULL AUTO_INCREMENT,
`uname` VARCHAR(45) NULL,
`upass` VARCHAR(45) NULL,
`email` VARCHAR(45) NULL,
PRIMARY KEY (`uid`));

Create MYSQL Database connection with Java

Create a class name as “MyDb.java” that will be responsible to create a database connection with MySQL. Below is the code for “MyDb.java”. this class contains method “getCon()”  that will return a MySQL connection.

Steps to create database connection with Java

  • Define a Connection variable public Connection con;
  • Load MYSQL driver Class.forName("com.mysql.jdbc.Driver");
  • Create a connection with MYSQL DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "root");
import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

public class MyDb {

public Connection con;

public Connection getCon(){

try {

Class.forName("com.mysql.jdbc.Driver");

con =  DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "root");

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return con;

}}

Create a registration form in JSP

Create a JSP or HTML file with the name “registration.jsp”. this file contains HTML elements and an HTML form to design a registration form and when the user clicks on the registration button the request will be sent to “Main.java” which is defined into the action attribute of the form.

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="Main">
<pre>

Name :<input name="name" type="text">

Password:<input name="pass" type="text">

Email:<input name="email" type="email">

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

</pre>
</form>
</body>
</html>

Create a Login form in JSP

Create another HTML form to perform login activity. Let’s create one more JSP or HTML file with the name “login.jsp or login.html”,  this view(JSP file) contains two input fields and a login button. when the user enters the user name and password and hits the login button the request will be sent to “Login.java”

<body>
<pre>
<form method="post" action="Login">
<input type="text" name = "uname">

User Name : <input type="text" name = "upass">

User Pass : <input value="Submit" type="submit">
</form>

</pre>
</body>

Create a servlet for registraiton form

Create a servlet with the name “Main.java” that will get requests from “registration.jsp” and create an object of MyDb class to create a connection with MySQL and send the data to the MYSQL user table.

String name = request.getParameter("name");

String pass = request.getParameter("pass");

String email = request.getParameter("email");

MyDb db = new MyDb();

Connection con = db.getCon();

try {

Statement stmt =  con.createStatement();

stmt.executeUpdate("insert into user (uname,upass,email)values('"+name+"','"+pass+"','"+email+"')");

out.println("data inserted sucessfully");

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

Now, the Registration Part is Completed now will code for the login part.

Create a servlet for login form

Create a servlet with the name “Login.java” that will get requests from “login.jsp” and create an object of MyDb class to create a connection with MySQL and check the username and password is available or not in the user table. If the user exists it will be redirected to the welcome page otherwise it will send an error message. “Login is failed”.

String name = request.getParameter("uname");
String pass = request.getParameter("upass");
MyDb db = new MyDb();
Connection con = db.getCon();
try {
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from user where uname = '"+name+"'");
if(rs.next()){
out.println("Login Sucessfully");
}

} catch (SQLException ex) {
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}

I hope it’s now clear to you. how to create a login and registration page and java database connectivity(JDBC) with MySQL.

More Java Practise Tasks

  • How to send and receive mail in Java
  • How to find file path in Java
  • Insert data from the single page into multiple tables
  • Viva Questions and answers for java project
  • Check new projects in java
PrevPreviousJava web project (Online Hotel Booking)
NextJSP tutorial with examplesNext

Recent Post

How to Explain Your Automation Testing Project in an Interview

December 1, 2024

Automation Testing Interview Questions for Wait in Selenium C Sharp

December 1, 2024

Automation Task: Extracting Product Names and Ratings with Selenium in C#

November 25, 2024

Space Booking Project in java using JSP and Servlet with source code

November 23, 2024

Design Test Automation Framework using Java, Selenium, TestNG, POM

August 1, 2024

Bug Reporting Project using Spring Boot, Hibernate, JPA and Mysql

July 8, 2024

Cannot find the declaration of element ‘project’ in Pom.xml

May 8, 2024

Failed to configure a DataSource: ‘url’ attribute is not specified

May 8, 2024

Solve “Non-resolvable parent POM” Error in pom.xml with Spring Boot 3

May 8, 2024

JSP and Servlet Project Configuration Tutorial

May 2, 2024

CODEBUN

  • jcodebun@gmail.com
  • +91 8827363777
Facebook Twitter Youtube

Automation

  • Who We Are?
  • Services And Plan
  • Contact Us
  • Privacy Policy
  • Terms And Conditions
  • Internship

Programs

  • Java program
  • VB Program
  • C# Program

Automation

  • Selenium Tutorial
  • Selenium Web Driver With Java
  • Selenium Web Driver With C#
  • Katalon studio
  • Puppeteer and Jest