Skip to main content

MVC Architecture in Java

MVC Architecture in Java

In this post we will understand and learn about MVC architecture in Java. We will also learn how to implement web application using using JSP and servlets technologies. MVC  is an open architecture with multiple tiers. As a java developer must have strong knowledge on MVC architecture,when ever you faced technical round interview question as experienced developer you should expect the question like tell me about your project architecture,so you should be in a position to answer this questions.

MVC is a design pattern stands for Model,View and Controller. It can be used to design web applications in a standard manner that means  it will provide a pattern to design web applications. It can able to provide the roles and responsibilities of the components which we used in MVC pattern or architecture.

Model:  Here model is a one of the component in MVC pattern. It maintains the business rules of your application and interact with the persistence storage to store,retrieve and manipulate data. This model data should not dependent on any struts or JSP/servlet API

View: It is also one of the component in MVC pattern. It's responsible for displaying the results back to the browser or user. That means what the end user sees. The view should not modify model data directly. Generally view layer is implemented by using JSP

Controller: The controller receives the request from web browser and select appropriate view to return. Here Controller layer is implemented using Action servelet

Why need to use MVC architecture:

We are using MVC design pattern to develop web applications.It is good to develop your applications where application data and business rules are separated from particular client implementation. 

Now we will see the flow of execution of the MVC architecture as follows:






Step 1: When ever we submitting the request from client(Browser) to Server component then controller receives the request from Browser. So you must remember every request will reach to controller component.

Step 2: The controller receives the request from Browser then Controller has to identify a particular model. Then it will execute business logic.Here model is might be java class or business logic's available. Here controller acts as mediator between view and the model. Finally, after performing some action on server side then controller identify respective view component. That means JSP page has view part.

Step 3: After executing model part if it is required to interact with database then model part interact with database.

step 4: Then view layer displays of the model to the end user. Then generate the require dynamic webpage return to Browser. Finally view part send to the response to the client(Browser)

Now we will see Rules we need to follow to develop MVC based applications:

1) We should use a servlet as controller only and JSP as View layer only. we should not interchange of those components.

2) We should send all request to controller servlet only.

3) The model should not dependent on any class

4) The controller should not interact with database directly. It must interact through model only.

5) we should not provide page to page communication in jsp pages. All jsp pages should communicate with controller then JSP page.

6) Jsp pages have only get the data from the Model layer but could not set the data into the Model layer.


I hope you people enjoy the post,share this to your friends and social websites to reach your dearest friends too. Keep follow me for latest updates and let me know your valuable opinions on this concept. 


Recommend to Read following Topics:

JSP Life Cycle
Struts Interview Questions and answers
How Cookies Works in Servlet
Java Hashmap with Examples

Comments

Popular posts from this blog

Mockito interview Questions

1.       Question 1. What Is Mockito? Answer : Mockito allows creation of mock object for the purpose of Test Driven Development and Behavior Driven development. Unlike creating actual object, Mockito allows creation of fake object (external dependencies) which allows it to give consistent results to a given invocation. 2.       Question 2. Why Do We Need Mockito? What Are The Advantages? Answer : Mockito differentiates itself from the other testing framework by removing the expectation beforehand. So, by doing this, it reduces the coupling. Most of the testing framework works on the "expect-run-verify". Mockito allows it to make it "run-verify" framework. Mockito also provides annotation which allows to reduce the boilerplate code. 3.       Question 3. Can You Explain A Mockito Framework? Answer : In Mockito, you always check a particular class. The dependency in that class is injected using mock object. So, for example, if you have service class

JAVA Expert Interview Questions Answers 2017

Java Basics ::  Interview Questions and Answers Home  »  Interview Questions  »  Technical Interview  »  Java Basics  » Interview Questions 1.     What is the difference between a constructor and a method? A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator. A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator. 2.     What is the purpose of garbage collection in Java, and when is it used? The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used. 3.     Describe synchronization in respect to multithreading. With respect to

Java Example Program to Convert List to Set

import java.util.ArrayList; import java.util.HashSet; import java.util.Set; public class ListToSet {  /**   * @author Amarjit Kumar   * @category interview questions   *   * Description: Convert List to set in java with example program   *   */  public static void main(String[] args) {   ArrayList<String> arrList= new ArrayList<>();   arrList.add("Java");   arrList.add("Java");   arrList.add("List to String");   arrList.add("Example Program");   Set<String> strSet = new HashSet<String>(arrList);   System.out.println(strSet);   System.out.println(arrList);  } } /*  * Java program to convert list to set. Convert ArrayList of string to HashSet  * in java example program How to convert List to Set in java Set<String> strSet  * = new HashSet<String>(arrList); HashSet having a constructor which will take  * list as an argument. Lets see how to convert list to Set using java program.