×

MVC in Java

A well-known design pattern is Model-View-Controller. The discipline of web development. We can organize our code in this manner. The document stipulates that a program or application must include a data model, display information, and control information. All these elements must be divided into separate objects to follow the MVC design.

Model-View-Controller Pattern is also known as the MVC pattern. This pattern is employed to divide the concerns of an application.

• Model: Model depicts a data-carrying object or Java POJO. Additionally, data changes may include logic to update the Controller.

• View: View represents how the model's data is visualized.

• Controller: The Controller affects both the View and the model. It refreshes the display and manages the data flow into the model object whenever data changes. View and model are kept apart.

The Model in Java programming comprises the basic Java classes, the Controller houses the servlets, and the View is used to show the data.

As a result of this division, the user requests are handled as follows:

1. The Controller on the server side receives a request for a page from the client.

2. The model is then called by the Controller. It compiles the needed information.

3. The data is sent from the Controller to the view layer.

4. The View is now returning the result to the client, which is the browser.

Benefits of the MVC Architecture

1. Scalability is a property of MVC that aids in expanding applications.

2. There is minimal dependence, making maintenance of the components simple.

3. Multiple views can reuse a model, allowing code reuse.

4. The developers can all work on the three layers—Model, View, and Controller—at once.

5. The program is made easier to comprehend by using MVC.

6. Each layer is maintained individually using MVC, so we don't need to deal with a tone of code.

7. It is simpler to test and extend applications.

Java implementation of MVC

We'll make an Employee object that will serve as a model. EmployeeView will be a view class that may publish Employee information on the console. EmployeeController is the controller class in charge of storing data in the Employee object and updating the View EmployeeView as necessary.

MVC Program in Java

EM.java

public class EM { 
// Declaring the variables
private String employee_name; 
private String employee_id; 
private String employee_department; 
// getId() method is used to get the id
public String getId() { 
return employee_id; 
} 
// setId() method is used to set the id 
public void setId(String id) { 
this.employee_id = id; 
} 
// getName() method is used to get the name
public String getName() { 
return employee_name; 
} 
// setName() method is used to set the name
public void setName(String name) { 
this.employee_name = name; 
} 
// get department() method is used to get the Department 
public String getDepartment() { 
return employee_department; 
} 
/ setDepartment() method is used to set the Department 
public void setDepartment(String Department) { 
this.employee_department = Department; 
} 
} 

EV.java

public class EV { 
public void printEmployeeDetails (String EmployeeName, String EmployeeId, String EmployeeDepartment){ 
System.out.println("Employee Details: "); 
System. out.println("Name: " + EmployeeName); 
System.out.println("Employee ID: " + EmployeeId); 
System.out.println("Employee Department: " + EmployeeDepartment); 
} 
} 

EC.java

public class EC { 
private Em model; 
private EV v1; 
public EC(Em model, EV v1) { 
this.model = model; 
this.v1 = v1; 
} 
public void setEmployee_name(String name){ 
model.setName(name); 
} 
public String getEmployee_name(){ 
return model.getName(); 
} 
public void setEmployee_id(String id){ 
model.setId(id); 
} 
public String getEmployee_id(){ 
return model.getId(); 
} 
public void setEmployee_department(String Department){ 
model.setDepartment(Department); 
} 
public String getEmployee_department(){ 
return model.getDepartment(); 
} 
public void updateView() { 
v1.printEmployeeDetails(model.getName(), model.getId(), model.getDepartment()); 
} 
}

MVC1.java

public class MVC1{ 
public static void main(String[] args) { 
// retrieving the employee record from the database using the employee id 
Em m1 = retriveEmployeeFromDatabase(); 
// making a view to display employee information on the console 
EV v1 = new EV(); 
EC c1 = new EC(m1, v1); 
c1.updateView(); 
c1.setEmployee_name("kamal"); 
System.out.println("\n Details of employee after updation: "); 
c1.updateView(); 
} 
private static Em retriveEmployeeFromDatabase(){ 
Em E1 = new Em(); 
E1.setName("Rohith"); 
E1.setId("30"); 
E1.setDepartment("manager"); 
return E1; 
} 
} 

Output

MVC in Java

Program2 For MVC in Java

Student1.java

public class Student1 {
private String roll_no;
private String name;
public String getRollNo() {
return roll_no;
}
public void setRollNo(String roll_no) {
this.roll_no = roll_no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

SV.java

public class SV {
public void printStudentDetails(String studentName, String studentRollNo){
System.out.println("Student: ");
System.out.println("Name: " + studentName);
System.out.println("Roll No: " + studentRollNo);
}
}

SC.java

public class SC {
private Student1 model;
private SV v1;
public SC(Student1 model, SV v1){
this.model = model;
this.v1 = v1;
}
public void setStudent_name(String name){
model.setName(name); 
}
public String getStudent_name(){
return model.getName(); 
}
public void setStudent_rollNo(String rollNo){
model.setRollNo(rollNo); 
}
public String getStudent_rollNo(){
return model.getRollNo(); 
}
public void updateView(){ 
v1.printStudentDetails(model.getName(), model.getRollNo());
} 
}

MVC2.java

public class MVC2 {
public static void main(String[] args) {
Student1 m1 = retriveStudentFromDatabase();
SV v1 = new SV();
SC c1 = new SC(m1, v1);
c1.updateView();
c1.setStudent_name("Rohith");
c1.updateView();
}
private static Student1 retriveStudentFromDatabase(){
Student1 s1 = new Student1();
s1.setName("kamal");
s1.setRollNo("10");
return s1;
}
}

Output

MVC in Java

Related Topics

Java Type Casting

Type casting is a technique or process used in Java to convert one data type into another, either manually or automatically. The compiler performs the automatic conversion, and the programmer...

3 minutes read.

How to check valid date in Java?

Every time we get data for any application, we must first ensure that it is accurate before continuing with any further processing. We might have to confirm the following while dealing...

4 minutes read.

How to Convert char to int in Java

How to Convert char to int in Java When two variables of different types are involved in the single expression, Java compiler uses built-in library function to convert the variable to...

3 minutes read.

Dining Philosophers problem in Java

The Problem of the Dining Philosophers illustrates a concurrency issue involving the distribution of scarce resources among conflicting processes. Problem Statement Imagine a dining table with a circle in the middle and five...

3 minutes read.

Java Control Statements

Control Statements: Control statements in Java can also be referred to as decision-making while dealing with different problems. Control statements are helpful to sort out the flow of the program or...

7 minutes read.

What is advance Java?

The definition of advance inside the dictionary refers to a forward motion, development, or progress, and the definition of enhancing is something that improves things. To become experts in that...

3 minutes read.

Resultset in java

Resultset: A result set is an interface that is present in the package java.sql and the resultset is used to store the data that are returned from the database table after...

5 minutes read.

Java Integer toHexString() method

The toHexString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 16. Syntax public static String toHexString (int  i)  Parameters The parameter ‘i’ represents...

1 minute read.

Dangling Else problem in Java

A language interpretation uncertainty is the hanging other issue. The following two types of condition executed code are both possible in programming: 1. if-then-else form 2. if-then form When dealing with the nested...

3 minutes read.

How to check version of java in Linux

Java is one of the most famous and thoroughly utilized programming tongues from one side of the world to the other. On the off chance that you are a Java...

2 minutes read.

Character Array in Java

A character array is an array which holds values of character data types. It is different from a string array. The character array, string, and StringBuffer classes in the Java...

4 minutes read.

Java String charAt() method

It returns the char value present in the string at the specified index. Here, index value can not be greater than length() -1. Syntax: public char charAt (int index) Parmeters It accepts only...

3 minutes read.

How to find the length of an Array in Java

Arrays: An array is a sort of container object that stores constant quantities of values of a single type in one memory area. A finite number of items must all be...

3 minutes read.

Java this keyword

This Keyword in Java This keyword can be used in many different ways in Java. This is a reference variable in Java that points to the active object. In Java, the...

8 minutes read.

XOR Binary Operator in Java

One of the various Bitwise operators in Java is ava XOR. If two boolean operands are given, the XOR (also known as exclusive OR) returns true. When both of the...

4 minutes read.

Java Database Connectivity with Oracle

JDBC: A Programmer can develop a complete application using the Java built-in API’s. So, for storing the data required for solving a real-world problem is stored into a database. To connect...

5 minutes read.

Java Extends vs Implements

Java: We known that the java is a pure object oriented programming language. Java programming language consists of many features such as portable, plat form independence, secured, robust, simple, architecture neutral,...

4 minutes read.

Java Class Name

How to write a class name? The following considerations should be made while writing class names. The name of the current class shouldn't be based on a preset or existing class. Java keywords...

3 minutes read.

Add Time in Java

Before Java 8, java.util.Date was one of the most usually involved classes for addressing date-time values in java. Then Java 8 presented java.time.LocalDateTime and java.time.ZonedDateTime. Java 8 likewise permits us...

6 minutes read.

Constructor Overloading in Java

In Java, constructors can be overloaded just like methods. The idea of having multiple constructors with various parameter sets so that each function Object()  can carry out a particular task...

3 minutes read.