Login Web Application Using Java MySql JSP and Servlet

Login Web Application Using Java MySql  JSP and Servlet

In this article, we are going to develop a complete Login web application using java, Mysql, JSP and servlet. We will be using MySQL for Database, JSP for frontend, Servlet for Backend. Donwload Full Code here

Things Covered are,

  1. New User Registration
  2. User Login
  3. User Logout
  4. Creating Servlet
  5. FrontEnd using JSP and Html

Pre-Requirements:

  • Mysql
  • Apache tomcat
  • Eclipse IDE
  • Create Dynamic Web Project
  • Add these jar in Java Build path and deployment Assembly

jasper.jar,  jasper-el.jar,  jsp-api.jar, servlet-api.jar

  • Add mysql connector in WebContent/WEB-INF/lib

1. Create Database and Table

Login to mysql and create Database/Schema using following command


CREATE DATABASE myschema;

Column Definition ,

  • name – Used to store user name
  • mailid – Used to store user mail id
  • password – Used to store password
  • userid – Unique id generated for each user. It help us to identify particular user.

 


CREATE TABLE myschema.Users (
name varchar(255) NOT NULL,
mailid varchar(255) NOT NULL,
password varchar(255) NOT NULL,
userid INT(10) auto_increment NOT NULL,
CONSTRAINT Users_PK PRIMARY KEY (userid)
)
ENGINE=InnoDB
DEFAULT CHARSET=utf8
COLLATE=utf8_unicode_ci;

2.Create index.html

Create index.html file inside WebContent folder. This is the first user see before the registration or login

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Wisdom Overflow</title>
</head>
<h1>Welcome to Wisdom Overflow!</h1>
<body>
<li><a href="register.jsp"> Register</a></li>
<li><a href="Login.jsp"> Already User? Login Here</a></li>
</body>
</html></pre>
<pre>

3. Add register.jsp

Create register.jsp inside WebContent Folder. New user register in  this page.

Username, email and password are the required fields. Once submit is clicked, user data is passed to regcheck.jsp for validation.

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Registration Page</title>
</head>
<body>
<form action="regcheck.jsp" method="post">
<table border="0">
 <tr>
 	 <th> Registration Page </th></tr>
 <tr>
  	<td>UserName</td>
  	<td><input type="text" name="username" value="" placeholder="enter name" required></td>
 </tr>
 <tr>
  	<td>Email</td>
  	<td><input type="email" name="mailid" value="" placeholder="enter email id" required></td>
 </tr>
 <tr>
	<td>Password</td>
	<td><input type="password" name="password" value="" placeholder="enter password" id="pwd" required></td>
 </tr>

</table>
<input type="submit" value="submit">
</form>

</body>
</html>

 

4. Create regcheck.jsp

User details are validated in regcheck.jsp.

If user registration is successful success message is shown to the user.

<%@page import="javax.swing.JOptionPane"%>
<%@page import="java.sql.Connection"%>
<%@page import="com.db.DBConnection" %>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String user = request.getParameter("username");
String password = request.getParameter("password");
String mailid = request.getParameter("mailid");
DBConnection dbconnect = new DBConnection();
Connection con = dbconnect.connect();

PreparedStatement preparedStatement = con.prepareStatement("Select * from Users where mailid= ?");
preparedStatement.setString(1,mailid);
ResultSet rs = preparedStatement.executeQuery();
//Check if user already exists with particular mail id
if(rs.next())
{
	JOptionPane.showMessageDialog(null,"User Already exists");
	response.sendRedirect("register.jsp");
	return;
}

a
preparedStatement = con.prepareStatement("insert into Users (name,password,mailid)values(?,?,?)");
preparedStatement.setString(1,user);
preparedStatement.setString(2,password);
preparedStatement.setString(3,mailid);
int result = preparedStatement.executeUpdate();
if(result != 0)
{ 
	JOptionPane.showMessageDialog(null,"success");
	response.sendRedirect("index.html");
}
else
{
	JOptionPane.showMessageDialog(null, "failed");
	response.sendRedirect("register.jsp");
}
%>
</body>
</html>

5. Create DBConnection.java

Now Create this package src/com/db and create DBConnection.java. This package is used to get DB connection. We are using Mysql DB and mysql connector to connect with DB. To connect with DB root password is must.

package com.db;
import java.sql.*;
import java.sql.DriverManager;
import java.util.Properties;
public class DBConnection {

    
    private static final String DATABASE_DRIVER = "com.mysql.cj.jdbc.Driver";
    private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/myschema"; //instead of 'myschemma' use your database name
    private static final String USERNAME = "root";
    private static final String PASSWORD = "";
    private static final String MAX_POOL = "250";

    
    private Connection connection;
 
    private Properties properties;

    private Statement statement;
    
  

    /**
     * Connect to the database  
     * 
     * @return Connection
     */
    public Connection connect() throws Exception
    {
    
        if (connection == null)
        {         
            try
            {
            	  Class.forName("com.mysql.cj.jdbc.Driver"); //mysql connector 8.0.22
            	   connection     =   DriverManager.getConnection("jdbc:mysql://localhost/myschema?" +  "user=root&password=");
           
            }
            catch ( Exception e)
            {
                e.printStackTrace();
            }
        }
        return connection;
    }

    /**
     * Disconnect database
     */
    public void disconnect()
    {
        if (connection != null)
        {
            try
            {
                connection.close();
             
                connection = null;
                
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
    }
}

 

 

Here is the screenshots for Login Web Application Using Java

Index Page

Index Page Screenshot

Registration Page

Registration Page

Check out the part 2 of this tutorial:

Login Web App Using Java  Mysql JSP Servlet Part-2

Check my Github Repository for full code

Sree Hari Sanjeev

The founder and CEO of Wisdom Overflow. His enthusiasm and effort has taken the blog to next level. You will be motivated just by taking a look at his daily schedule.

Leave a Reply

Your email address will not be published.

3 + 15 =