Building a Seamless User Registration and Login Workflow in a Java Web Application

Java Web Application for New User Registration and Login

Description: This example is in continuation to my previous Blog “First Java Web Application using Netbeans, Apache Tomcat, MySQL”. You can find that Blog Here: https://medium.com/@hiyasdrive/first-java-web-application-using-netbeans-apache-tomcat-mysql-cb8e93f3e407

In this example I have tried to demonstrate a very common workflow for new User Registration on a website, in which the user fills a registration form with required personal information along with the preferred username and password. When submitting the form an email is triggered to the email ID shared by the user to confirm the user identity. User opens his/her email and clicks on the activation link. With this action the application verifies and activates the user on the website. Once the verification is complete, the user can login to the website using his/her username and password.

Complete Project Source Code on GitHub:

https://github.com/hiyasharma/LoginApp-2.0

Learning Objectives:


Developing workflow for New User Registration on your website

Developing User Login procedure

Making use of Gmail SMTP for sending emails

MySQL Database Read/Write operations

References:

Procedure to generate Gmail App Password in order to use Gmail SMTP for sending emails through your Java Web application : https://support.google.com/mail/answer/185833?hl=en.

Since Google does not allow you to use Gmail SMTP directly through your normal Username and Password, rather Google recommends using Gmail APIs for this purpose. Making use of Gmail API would require you to use OAuth (Open Authentication) in your application. Since OAuth is a lengthy process hence in this example I am using Gmail SMTP with my username and App Password which is generated on my Google Account. I will share more examples in future to demonstrate OAuth and Gmail APIs.

Technology:

Apache NetBeans IDE 23

Java 22.0.1 64-Bit Server

Java SE Runtime Environment 22.0.1

OS Windows 11

Web Server Apache Tomcat 11.0.1

Servlet Version: 6.1

JSP Version: 4.0

MySQL Community Server 8.0.20

Detailed Procedure:

To setup the project Environment please refer the previous Blog https://medium.com/@hiyasdrive/first-java-web-application-using-netbeans-apache-tomcat-mysql-cb8e93f3e407 Environment Setup Requirements:

Ensure that your MySQL setup is complete:

Create database with name devsprintdb and create tables usersregistration and profiles:

CREATE DATABASE `devsprintdb;

CREATE TABLE `users` (
`ID` double NOT NULL AUTO_INCREMENT,
`USERNAME` varchar(50) DEFAULT NULL,
`USERTYPE` varchar(50) DEFAULT NULL,
`PASSWORD` varchar(100) DEFAULT NULL,
`ACTIVE` tinyint unsigned DEFAULT NULL,
`CREATED_AT` timestamp NOT NULL DEFAULT '0000–00–00 00:00:00',
`UPDATED_AT` timestamp NOT NULL DEFAULT '0000–00–00 00:00:00',
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=39 DEFAULT CHARSET=latin1;

CREATE TABLE `registration` (
`ID` double NOT NULL AUTO_INCREMENT,
`USER_ID` double DEFAULT NULL,
`FIRST_NAME` varchar(100) DEFAULT NULL,
`LAST_NAME` varchar(100) DEFAULT NULL,
`EMAIL` varchar(100) DEFAULT NULL,
`SECURITY_QUESTION` varchar(200) DEFAULT NULL,
`SECURITY_ANSWER` varchar(100) DEFAULT NULL,
`CREATED_AT` timestamp NOT NULL DEFAULT '0000–00–00 00:00:00',
`UPDATED_AT` timestamp NOT NULL DEFAULT '0000–00–00 00:00:00',
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=56 DEFAULT CHARSET=latin1;

CREATE TABLE `profile` (
`ID` double NOT NULL AUTO_INCREMENT,
`USER_ID` double DEFAULT NULL,
`FIRST_NAME` varchar(100) DEFAULT NULL,
`LAST_NAME` varchar(100) DEFAULT NULL,
`CREATED_AT` timestamp NOT NULL DEFAULT '0000–00–00 00:00:00',
`UPDATED_AT` timestamp NOT NULL DEFAULT '0000–00–00 00:00:00',
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=32 DEFAULT CHARSET=latin1;

Following are the code files needed in this application, for which the source code is available at the GitHub Repository:

JSP Files:

  1. index.jsp
  2. login_success.jsp
  3. login_error.jsp
  4. registration.jsp
  5. registration_success.jsp
  6. activation_success.jsp

index.jsp : Is the main landing page of the application through which we can login or create a new user with New User Registration.

login_success.jsp : In case of successful login this page is displayed.

login_error.jsp : In case of unsuccessful login this page is displayed.

registration.jsp : This page provides the User Registration Form for submission.

registration_success.jsp : This page is displayed when the User Registration is successful and verification email is sent with activation URL.

activation_success.jsp : This page is displayed indicating the successful activation when you click the activation URL in the email.

All the above mentioned JSP files shall be created under the folder Web Pages as shown below:

LoginApp-2.0

↳ Web Pages

Since index.jsp is the first page to be displayed on the browser hence ensure the below code present in web.xml:

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

Java Class Files:

Description of Java Code files:

RegistrationDAO.java : This class provides the method addNewRegistration(). In this method the User Registration values are entered into the database.

UserDAO.java : This class provides the methods userLogin() — for login procedure and activateUser() — for updating the active flag into the database.

RegistrationBean.java : Represents the table registration in the database with getter and setter methods.

UserBean.java : Represents the table user in the database with getter and setter methods.

activateRegistrationServlet.java : This servlet is called when a user clicks on the activation link provided in the verification email.

loginServlet.java : This servlet is called at the time of user login.

registrationServlet.java : This servlet is called when a user submits the New User Registration form.

MailUtility.java : This class provides the method sendMail() for sending email.

Project Object Model (pom.xml) to ensure given inclusion of Java dependencies :

<dependencies>
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-api</artifactId>
<version>${jakartaee}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.32</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mailapi</artifactId>
<version>1.4.3</version>
<type>jar</type>
</dependency>
</dependencies>

Deployment Descriptor (web.xml) — ensure the following inclusions:

<servlet>
<servlet-name>registrationServlet</servlet-name>
<servlet-class>Servlets.registrationServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>activateRegistrationServlet</servlet-name>
<servlet-class>Servlets.activateRegistrationServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>Servlets.loginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>registrationServlet</servlet-name>
<url-pattern>/registrationServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>activateRegistrationServlet</servlet-name>
<url-pattern>/activateRegistrationServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

Application Workflow:

On successful compilation and deployment (i.e. Clean and Build > Run in case of NetBeans) the following page is displayed on your browser:

On the top right corner there is an option “New Member Registration”. Click on this hyperlink — it will take you to the New Member Registration page:

Fill all the field values. Make sure to provide an active Email ID since an email will be sent to this ID for verification. Also make sure to enter the same password correctly two times.

After providing all the details you have to click the “Submit” button.

After a few seconds you will get the message “Your new member registration is successful. E-Mail message has been sent to your mailbox. Check your mail and activate your account.” as shown above. This indicates that your registration information has been accepted and a confirmation email is sent with the activation URL.

You need to open your mailbox and check the email. Click on the activation link which is similar to :

<A HREF=’http://localhost:8080/activateRegistrationServlet?username=hiyasdrive5'> Click here to activate your account</A>

Once you click on this URL you will see the message as below:

User activation Successful!! Click here to Login

Click on the link and you will go back to the Login Page. There you can provide your newly created Username and Password. In case your credentials are correct you will get the page as below for successful login:

In case the credentials are incorrect you will get the following page indicating unsuccessful login:








Comments

Popular posts from this blog

Building Your First Java Web Application: A Step-by-Step Guide