Practice MVC with Java EE

Overview

In this project, I implemented J2EE Model-View-Controller (MVC) design pattern to develop a user login application. The application prompts users to provide their login credentials and validates them by comparing them to the data stored in the database. If the validation is successful, a success message is displayed to the user. In the event of incorrect credentials, an error message is presented to the user.

Functionalities include:

Source Code Link

Project Walk-Through

(Model) UserDAO.java

This java class's responsibility is to interact with the databse. Upon class initialization, a connection to the database will be established. When validateUser method is called, the program will SELECT from the user table and return the first record.

Please note that for simplicity, the database access credentials are stored directly in the code.

(Model) UserModel.java

This class is created to store user information. In this case, each UserModel instance will be used to store a username and a password.

(Controller) UserController.java

This is a servlet where the main business logic lies. When this servlet gets called, a database connection will be created. Upon receiving a POST request, the method doPost will be executed. 

In doPost method, 

(View) login.jsp

Display a web form to a user, and send the data to UserController servlet using POST request.

(View) success.jsp

The webpage to which the user will be directed if entered username/password matches a record in the user database.

(View) error.jsp

The webpage which will be displayed to user if no record matches the username/password the user provided.

Demonstration

Setup Database with one user

Create a database and a table with two columns. Add one record to the table.

Test logging in with the correct credential

Upon entering the correct username and password, access to the website will be granted.

Test logging in with wrong credential

If the username and password pair don’t match any record in the database, the user will be redirected to a page that displays an error message.