HomeJSP

Model view controller design pattern in java benefits with example

Like Tweet Pin it Share Share Email

Model view controller design pattern in java is a very important concept for developers. Model view controller (MVC) pattern is used for developing large web applications. In this article we will discuss about model view controller. Throughout the article we will discuss the benefits of using model view controller in a java web applications. To understand this design pattern we will understand student registration example. In this example we will see model view controller pattern reduces complexity of student registration web application.

Model view controller design pattern definition :

Model View Controller alias MVC is a way of designing a large web application. MVC separates data access, business logic and the graphical user interface.

mvc Model View Controller Sitenol
mvc-model

image credit

Why should we not use JSP alone for developing large web applications? Reasons are here :

In JSP we can write all our java (Business Logic, View binding) codes. We know the power of JSP scriptlets (<% %>). JSP has everything needs to develop a web application, we are going to discuss the use of Model-View-Controller pattern.

Have you ever developed a 10 dynamic pages in JSP? If not, it would be a bit harder to feel and understand the Model view controller design pattern alias MVC. Suppose you want to develop a web application using JSP. Scriptlets will come in your mind and you would get ready to go with it. There are chances where it will increase your complexity.

Let us take some project examples.
1. Designing an Ecommerce website
2. Designing online community websites
3. Designing online forums
4. Designing a hospital management web application

Just think to design these web applications using JSP. Write all your business, UI, View code  in JSP. You will face difficulty by writing all logics in JSP page. Your source code will become complex. Other developers who will work on your code will face problem in understanding it. I have some explanations about this code complexity.

Explaining Drawback of Scriptlets in JSP:

1. More Scriplets more complexity : Scriptlets make JSP pages difficult to read, understand and maintain. You will see lot of code inside <% %>. It will take a lot of time and effort for other developers to understand your code.

2. Not suitable for large team : In a project team every member has different ability. There may be one web designer, One business logic developer, one presentation developer. It will become complex for all developers to write their code in a single JSP page.

3. Scriptlet code is not reusable: Scriptlet code appears only in the JSP page which defines it. Suppose if same logic is required at other place in your code. You need to again copy all the required code and paste into new content.

4. Junit testing of Scriptlet code is difficult : Unit testing of scriptlet code is virtually impossible. Scriptlets are embedded in JSP pages. The only way to execute them is to execute the page and test the results.

5. Time consuming and Complex : Only using JSP need to spend more time to develop custom tags, scriptlets, expression in JSP.

6. It is not good for web applications.

MVC design pattern to overcome the problems with JSP

Note : Please remember model view controller does not limits the importance of JSP. It uses JSP as its powerful element. JSP is used as view.
To overcome the above mentioned issues we should follows MVC (Model-View-Controller) paradigm, which divides application into three interoperable components:

Model :

It is termed as business data, data carrier or persistent object. It updates its value with the help of view or JSP. Controller takes data from Model to save records into database, page navigation.

View :

JSP is used as view. View or JSP renders the contents of a model. JSP gets data from the model and presents the data to the user. View communicates with model and controller. User updates data and click action button. JSP updates changed data to model. Controller access changed data from model.

Controller :

User request goes to Controller (Servlet). In a web application, user inputs carried using HTTP GET and POST requests. Controller takes this request, communicate to Model to update user changed data. Controller interacts with business logic (EJB, Hibernate or other data persistence object) to update data in database. Business logic updates data in database successfully. Controller returns the requested page with updated data.

Model view controller design pattern (MVC) have numerous benefits:

Separation of design concerns : In model view controller presentation, controller and data persistence are loosely coupled. It makes behavior of application more flexible. Modifications to one component have minimal impact on other components. You can create new views without changing the model.

More easily maintainable and extensible : Good structure can reduce code complexity. As such, code duplication is minimized.
Promotes division of labor : Developers with different skill sets are able to focus on their core skills and collaborate through clearly defined interfaces.

Understanding an example of Model View Controller design pattern (MVC) – Students registration system application :

A simple example is an application that saves students registration record.

The Model would be a class that handles the students personal details (Student Name, Address, Date Of Birth and other details). The View would be a GUI (in our case JSP) made up of a text field where student will enter value for Student Name, Address, Date Of Birth and other details. View displays the records which the student has entered. View page contains submit button. User clicks submit button and student registration process begins. The Controller would be the button action listener.

1. Model will be updated with the the students details. 2. Controller will take values from model and save or update the records in database. 3. After saving records Controller provides the student records to View.
View and the Controller will be in the same class as we can simply combine them.

Model view controller design pattern diagram
Model view controller design pattern diagram

Image Credit

This simple example explains – how Model View Controller design pattern (MVC ) can reduce complexity of our development. This pattern is followed by almost all the java based web application developed today.

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *