Thursday, December 15, 2016

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

No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...