Login application in Java using MVC and MySQL

This Java application follows the MVC architecture and consists of Java Servlets, JSP. It uses a MySQL database server to access detailed user information. The input fields are checked with Javascript.

What is the Model View Controller Architecture?

A model (CAD) consists of application data and business rules, and a controller (servlet) acts as an interface between the views and the model. It mediates the input and converts it into commands for the model or display. A view (JSP) can be any view of output data, such as a table or graph, usually HTML or JSP pages.

Here, DAO is a data access object – this section focuses on business logic and database server connections and operations.

Use any IDE – an integrated development environment to minimize your work. If you haven’t used the IDE yet, get it. You’re gonna fall in love with him. I recommend the use of Eclipse for Java applications. Link to download Eclipse. It is an open source tool.

This application is explained in detail with accompanying comments and tested in the Eclipse IDE. Write a comment if you have difficulty understanding the request.

It is suggested to read:

It is recommended to separate the different components in the standard folder structure as shown below. Start programming with New Project -> Dynamic Project-like Web Application.

Fault! The file name is not specified.

Java connection application with MySQL implemented in Eclipse.

data-medium-file=https://i0.wp.com/krazytech.com/wp-content/uploads/Directory-Structure.jpg?fit=201%2C300&ssl=1 data-large-file=https://i0.wp.com/krazytech.com/wp-content/uploads/Directory-Structure.jpg?fit=284%2C424&ssl=1 load=lazy class=wp-image-1920 size-full title=catalogue structure src=https://i0.wp.com/krazytech.com/wp-content/uploads/Directory-Structure.jpg?resize=284%2C424&ssl=1 alt=Save directory structure for Java MVC authentication form width=284 height=424 data-recalc-dims=1/>.

Structure of the Eclipse directory with packages, XML and dependent JARs

The login details are sent from the Login.jsp page to the LoginServlet. When you click the Login button, the request will be redirected to the page in the form action tag, so here the request will be redirected to the LoginServlet.java class.

Log in, please.

<%@ Page language=java contentType=text/html; charset=ISO-8859-1 PageEncoding=ISO-8859-1%>

 

Application to connect to Java with MVC and MySQL

 

Top of Form

Username
Password
<%=(request.getAttribute(errMessage) == null)? request.getAttribute(errMessage)%>
 

Bottom of Form

 

RegistrationServlet.java

Package com.mvc.controller ;

Javax.io.IOException import;
import javax.servlet.servletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.httpServletResponse

Import com.mvc.bean.LoginBean ; Import
com.mvc.dao.LoginDao ;

the LoginServlet public class is expanding HttpServlet {

public LoginServlet() // standard builder
{
}

protected null and void doPost(HttpServletRequest request, HttpServletResponse response) launches ServletException, IOException {

// The username and password are the names I entered in the input field on the Login.jsp page. Here I retrieve the values entered by the user and save the variables for later use.

Row UserName = request.getParameter ;
Row Password = request.getParameter ;

LoginBean loginBean = new LoginBean(); // create an object for the LoginBean class, which is a regular Java class that only contains setters and gotters Bean classes are actually used in Java to access user information where needed in the application.

loginBean.setUserName(username); //set username and password via the object loginBean, only you can get it in the future.
loginBean.setPassword(password) ;

LoginDao loginDao = new LoginDao(); //create an object for LoginDao. This class contains the basic logic of the application.

String userValidate = loginDao.authenticateUser(loginBean); //call authenticationUser function

if(userValidate.equals(SUCCESS)) //Als the function returns the success string and the user is redirected to page
{
request.setAttribute(userName, username); // With setAttribute() you can set a few keys and values to get it later with getAttribute(key)
request.getRequestDispatcher(/Home.jsp).forward(request, response);// RequestDispatcher is used to send the control to the called page.
}
else
{
request.setAttribute(errMessage, userValidate);// As authenticateUser() the function returns a string in addition to the SUCCESS string, which is returned to the login page. Here the error message returned by the function is stored in the errMessage key.
request.getRequestDispatcher(/Login.jsp).forward(request, response);//Forward the request
}.
}
}.

Tao.java Registration

Package com.mvc.dao;

Import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.mvc.bean.LoginBean;
import com.mvc.util.DBConnection

LoginDao public class {
public String authenticateUser(LoginBean loginBean)
{
String userName = loginBean.getUserName(); //Add user enters values in temporary variables.
Password string = loginBean.getPassword() ;

Connection con = zero;
Instruction overview = zero;
Result Set Result Set = zero ;

String userNameDB = ;
String passwordDB = ;

try
{
con = DBConnection.createConnection(); //Fetch database connection object
statement = con.createStatement(); //Statement is used to write queries. Read more.
resultSet = statement.executeQuery (selection of username, user password); // Table name – username and name, password – columns. Receive all datasets and store them in the ResultSet.

while(resultSet.next()) // Until the following line appears, otherwise it will incorrectly give
{
usernameDB = resultSet.getString(username) ; // Returns the values that exist in the database
passwordDB = resultSet.getString(password) ;

if(username.equals(usernameDB) && password.equals(passwordDB))
{
returns SUCCESS; //// If the values entered by the user already exist in the database, this means that the user has already registered, so return the SUCCESS message.
}
}
catch(SQLException e)
{
e.printStackTrace();
}
return Incorrect user references; // returns the corresponding message if
} fails.
}
}.

LoginBean.java
JavaBeans are classes that encapsulate many objects in one object. This object facilitates access or installation for all members of the class. The class of beans generally only includes setters and growers.

package com.mvc.bean ;

LoginBean public class
{
private string user Name ;
private string password ;

public String getUserName() {
returns the username;
}
public invalid setUserName(String userName) {
this.userName = username;
}
public string getPassword() {
returns the password;
}
public invalid setPassword(String password) {
this.password = password;
}
}.

The name of the database is Clients and the name of the table is Users. When you watch the video, we use a MySQL database server which is part of the WAMP software (works for Windows). It’s free software, just download, install and use it. You can also download the MySQL server from the official website. You can also use any other database server.

Fault! The file name is not specified.

Script to create a user table for the Java login application

data-medium-file=https://i2.wp.com/krazytech.com/wp-content/uploads/2020/06/Script-to-create-users-table.jpg?fit=300%2C107&ssl=1 data-large-file=https://i2.wp.com/krazytech.com/wp-content/uploads/2020/06/Script-to-create-users-table.jpg?fit=400%2C143&ssl=1 load=lazy width=400 height=143 src=https://i1.wp.com/d3nqmfhxc38njt.cloudfront.net/wp-content/uploads/2020/06/Script-to-create-users-table.jpg?resize=400%2C143&ssl=1 alt=Script to create a user table for the Java application login class=wp-image-6671 data-recalc-dims=1/>Script to create a user table.

DBConnection.java
In this application we use a MySQL database. We can use any database server that supports Java. Depending on the database you choose, the correct drivers and connection URLs must be used.

Pay attention: Don’t forget to add a dependent bank for the database server. In our case, it’s a Java Micle connector.

Package com.mvc.util ;

Import java.sql.Connection;
Import java.sql.DriverManager

public class DBConnection {
public static connection createConnection()
{
connection con = null;
string url = jdbc:mysql://localhost :3306/client; //MySQL URL and then database name
string username = root; //MySQL username
string password = root123; //MySQL password
attempt
{
attempt
{
class.forName(com.mysql.jdbc.driver); //Driver load mysql
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
con = DriverManager.getConnection(url, username, password); //Try to connect to MySQL system
.out.println(print connection object +con);
}
catch (exception e)
{
e.printStackTrace();
}
return con;
}
}

web.xml
Web.xml is known as the installation description. It gives an overview of all servlets used in the application. Don’t forget to mention the full name of the servo class.

It has a number of additional parameters, such as the name of the welcome file, which leads to the filename mentioned when downloading this application.

LoginMvc

Login.jsp

LoginServlet
LoginServlet
com.mvc.controller.LoginServlet

LoginServlet
/LoginServlet

LogoutServlet
LogoutServlet
com.mvc.controller.LogoutServlet

LoginServlet
/LogoutServlet.

Home.jsp
Example for the homepage of a user after a successful login.

<%@ Page language=java contentType=text/html; charset=ISO-8859-1 PageEncoding=ISO-8859-1%>

 

Page

Welcome <%= Request.getAttribute(Username) %>.

Log out

 

LogoutServlet.java
This is an example of a logout mechanism. After clicking the Unsubscribe button the user will be redirected to Login.jsp.

Package com.mvc.controller ;

Import javax.io.IOException; Import
javax.servlet.RequestDispatcher; Import
javax.servlet.ServletException; Import
javax.servlet.httpHttpServlet;
javax.servlet.http.HttpServletImport RequestDispatcher;
javax.servlet.http.HttpServletImport Response;
javax.servlet.http.HttpServletImport Session ;

the public class LogoutServlet extends the HttpServlet{privacy static last long serialVersionUID =

protected void doGet(HttpServletRequest request, HttpServletResponse response) launches ServletException, IOException
{
HttpSession session = request.getSession(false) ; //Fetch session object

if(session!=null) // If the session is not null
{
session.invalidate(); // removes all attributes of the session associated with the request
setAttribute(errMessage, you have successfully logged out);
RequestDispatcher requestDispatcher = request.getRequestDispatcher(/Login.jsp);
requestDispatcher.forward(request, response);
System.out.println(Logged out);
}.
}
}.

Many users have requested a role-based login application. I recently wrote an article about an example of Java Logon based on session and role.

509 delta r3 carbon fiber ignite helmet,509 delta r4,best snowmobile helmet,509 helmets,509 delta r3 visor,509 black ops helmet,509 carbon fiber helmet,509 ignite goggles,delta r4 ignite helmet,where to buy 509 helmets,2020 snowmobile helmet,black helmet canada,royal distributing,509 delta r3 ignite carbon helmet,509 delta r3 ignite helmet,509 delta r3l ignite helmet,509 delta r4 ignite modular helmet hi vis,509 delta r3 r-series helmet

You May Also Like