This article describes the Java registration application that uses servlet, JSP, and MySQL database server. This Java registration form complies with the Model View Controller (MVC) architecture. Let’s discover what a CVM is and how to use it in the development of an application.
For logging into the Java application, read the article Logging into the Java application with MVC.
The Model-View-Controller (MVC) architecture is best explained using the following diagram.
Fault! The file name is not specified.
Implementation of the view controller design model in Java with connection to the JSP servlet database
Controller Model Type Architecture
For this Java application, we use the Eclipse Integrated Development Environment (IDE). To follow the basic encoding standards, I created the folder structure as shown in the following diagram.
Fault! The file name is not specified.
Eclipse directory structure for registering Java with the JSP servlet database
Read Also: Best Ps3 Games: Exclusive Collection of 2019
The eclipse directory structure for the Java Recording Application
It is suggested to read:
Every registration or registration request always starts with a viewing. It can be a page in HTML, JSP, PHP, VB, or other languages. The registration of our Java application starts with Register.jsp and from Register.JSP we call it the class RegisterServlet.java.
Register.jsp
<%@ Page language=java contentType=text/html; charset=ISO-8859-1 PageEncoding=ISO-8859-1%>
Java application for registration with MVC and MySQL
Top of Form
Name and surname | |
Electronic mail | |
Username | |
Password | |
Confirm password | |
<%=(request.getAttribute(errMessage) == null)? request.getAttribute(errMessage)%> | |
Bottom of Form
Registration form
RegisterServlet.java
Importing the com.mvc.controller package;
import javax.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.httpServletRequest;
import javax.servlet.httpServletResponse
Import com.mvc.bean.RegisterBean ;
Import com.mvc.dao.RegisterDao ;
the public class RegisterServlet extends the HttpServlet {
public registerServlet() {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) launches ServletException, IOException {
//Copy all input parameters to local variables
String fullName = request.getParameter(full name);
String e-mail = request.getParameter(e-mail);
String username = request.getParameter(username);
String password = request.getParameter(password) ;
RegisterBean registerBean = new RegisterBean() ;
/ Using Java Beans – The easiest way to play with group related data
registerBean.setFullName(fullName) ;
registerBean.setEmail(email) ;
registerBean.setUserName(userName) ;
registerBean.setPassword(password) ;
RegisterDao registerDao = new RegisterDao() ;
// The basic logic of the registration request is presented here. We will add user data to the database.
String userRegistered = registerDao.registerUser(registerBean) ;
if(userRegistered.equals(SUCCESS)) // If successful, display a message to the user on page
{
request.getRequestDispatcher(/Home.jsp).forward(request, response);
}
Other // If unsuccessful, display a meaningful message to the user.
{
request.setAttribute(errMessage, userRegistered);
request.getRequestDispatcher(/Register.jsp).forward(request, response);
}.
}
}.
In summary we can say that RegisterServlet.java flow :
1. Assign all user inputs (data) to the local variable.
2. Call RegisterBean.java to install all user data using the Java settings.
3. Then go to RegisterDao.java where you just have to enter the data of the user in the database.
4. As soon as the operation is successful, you will see a message about the successful result.
SaveDao.java
Data Access Object – focuses on business logic with database connections and operations
Here the RegisterDao.java code connects to the database layer and inserts the user’s data into the database.
Package com.mvc.dao;
java.sql.Connection import;
java.sql.PreparedStatement import;
java.sql.SQLException import;
com.mvc.bean.RegisterBean import;
com.mvc.util.DBConnection import ;
public class RegisterDao {
public string registerUser(RegisterBean registerBean)
{
string fullName = registerBean.getFullName() ;
string email = registerBean.getEmail() ;
string userName = registerBean.getUserName() ;
string password = registerBean.getPassword() ;
Connection con = null ;
PreparedStatement preparedStatement = null ;
try
{
con = DBConnection.createConnection() ;
String query = insert in user values (SlNo,fullName,Email,userName,password) (NULL, ?, ?, ?, ?); // insert user data in the ‘USERS’ table
prepareStatement = con.prepareStatement(request); // Use the statements prepared here to insert the
prepareStatement.setString(1, fullName);
prepareStatement.setString(2, Email);
prepareStatement.setString(3, userName);
prepareStatement.setString(4, password).
int i= preparedStatement.execUpdate() ;
if (i!=0) // Make sure the data are entered in the database
return SUCCESS;
}
catch(SQLException e)
{
e.printStackTrace();
}
return Oops Something went wrong in there… !
}
}
MySQL script to create a user table for the Java application for registration
SQL Script for creating the user table.
User table structure
RegisterBean.java
JavaBeans are classes that encapsulate many objects in one object (Beans). The JavaBean property is a named function that the user of the object can access. Here, RegisterBean includes the properties of register fullName, e-mail, userName, password. The installation and query functions are implemented for installation or access to individual properties.
package com.mvc.bean ;
public class RegisterBean {
private string for full name;
private string for e-mail;
private string for user name;
private string for password
public String getUserName() {
returns the username;
}
public invalid setUserName(String userName) {
this.userName = username;
}
public getPassword() {
returns the password;
}
public invalid setPassword(String password) {
this.password = password;
}
public invalid setFullName(String fullName) {
this.fullName = full name;
}
public String getFullName() {
gives full name;
}
public invalid setEmail(string email) {
this.email = email;
}
public string getEmail() {
gives email;
}
this.}
DBConnection.java
In this application we use a MySQL database server. The format of the URL and the name of the driver will be different for different database servers.
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/customers ; //MySQL URL followed by the database name
String username = root ; //MySQL username
String password = root123 ; //MySQL password
System.out.println(In the class DBConnection.java) ;
try
{
try
{
Klasse.forName(com.mysql.jdbc.Driver); // Download the MySQL driver. This is different for the database server}catch (ClassNotFoundException e){e.printStackTrace();}con = DriverManager.getConnection(url, username, password); // Try to connect to MySQL systemdatabase.out.println(print connection object +con);}catch (exception e){e.printStackTrace();}return con;}}}.
web.xml
Web.xml is known as the installation description. It shows which servlets exist and which URLs they work with. You must mention a fully qualified Servlet course here.
RegistrationMVC
Register.jsp
RegisterServlet
RegisterServlet
com.mvc.controller.RegisterServlet
RegisterServlet
/RegisterServlet
Homepage.jsp
<%@ Page language=java contentType=text/html; charset=ISO-8859-1 PageEncoding=ISO-8859-1%>
Page
Successful user registration
Log in to continue.
Page
The codebase can be downloaded here.