Model View Controller(MVC) Introduction

Introduction

A web application is a collection of static and dynamic web resources programs or client and server side resources or programs.

Static web resource programs(html) generate static contents whether as dynamic web resources(Servlet,JSP etc) generate dynamic web-pages.

A Typical Web Application Logic :

  • Presentation Logic :- Logic that generates user interface for end users.

  • Form Validation Logic :- Logic that verifies the pattern and format of data is called as form validation logic.It can be done at client side or server side.

  • Request Parameter gathering Logic :-Logic that can read all data from http request like header values,Request Parameter values etc is called as request data gathering logic.

Introduction

A web application is a collection of static and dynamic web resources programs or client and server side resources or programs.

Static web resource programs(html) generate static contents whether as dynamic web resources(Servlet,JSP etc) generate dynamic web-pages.

A Typical Web Application Logic :

  • Presentation Logic :- Logic that generates user interface for end users.

  • Form Validation Logic :- Logic that verifies the pattern and format of data is called as form validation logic.It can be done at client side or server side.

  • Request Parameter gathering Logic :-Logic that can read all data from http request like header values,Request Parameter values etc is called as request data gathering logic.





  • Session Management Logic :- The logic that remembers client data across the multiple requests during a session and makes web application as state-full application is called as session management logic.We use hidden forms,cookies,HttpSession and URL rewriting techniques for this purpose.

  • Business/Request Processing/Service Logic :- Main logic of application that generates result by using input values and performing calculation on these value is called business logic.

  • Persistence Logic :- Logic that interacts with database and manipulates the data by performing CURD operation is called as Persistence Logic.

  • Middleware Services Configuration Logic :- Middleware services are additional and optional configuration logic of the application.
    Example:- Security Service, Transaction Management, Connection Pooling etc.

An Example Program (Logic) of Web Application :

Request Data Gathering Logic

Read n1,n2,n3 empid etc details of Employee from form page.

Form Validation Logic

→ Validate whether Required fields are entered or not?
→ Validate whether fields are in proper format etc.

Business Logic

→ Total=n1+n2+n3
→ Average = Total/3
→ Generate Average income of Employee

Persistence Logic

Write Employee details to Database table as record.

Middleware Services

→ Apply Security Services on Application
→ Create Connection Pool to interact with database.



Different approaches of developing Java Web Application :

Model-1
Model-2

Model-1 Design :

Model 1 architecture is the easiest way of developing JSP based web applications. In Model 1, the browser directly accesses JSP pages. In other words, user requests are handled directly by the JSP.

In this approach we use either Servlet or JSP programs as server side resources in web application.

In case of Model 1 architecture the request is processed by JSP or Servlet. The JSP or Servlet handles all the responsibility of request processing such as validating the data, business logic validation and processing, show error messages, finally generating the view for the application.

Let us illustrate the operation of Model 1 architecture with an example. Consider a HTML page with a hyperlink to a JSP. When user clicks on the hyperlink, the JSP is directly invoked. This is shown in Figure below. The servlet container parses the JSP and executes the resulting Java servlet. The JSP contains embedded code and tags to access the Model JavaBeans. The Model JavaBeans contains attributes for holding the HTTP request parameters from the query string. In addition it contains logic to connect to the middle tier or directly to the database using JDBC to get the additional data needed to display the page. The JSP is then rendered as HTML using the data in the Model JavaBeans and other Helper classes and tags.

mvc overview

Advantage of Model 1 Architecture :

Easy and Quick to develop web application

Disadvantage of Model 1 Architecture :

  • Navigation control is decentralized since every page contains the logic to determine the next page. If JSP page name is changed that is referred by other pages, we need to change it in all the pages that leads to the maintenance problem.
  • Time consuming You need to spend more time to develop custom tags in JSP. So that we don't need to use scriptlet tag.
  • Hard to extend It is better for small applications but not for large applications.

Note : Use model 1 architecture for developing small scale web application which contains small number of web pages.

To solve all the problems of Model-1 architecture use Model-2 architecture(MVC).

Model 2 (MVC) Architecture :

Model 2 is based on the MVC (Model View Controller) design pattern. The MVC design pattern consists of three modules model, view and controller.

Model The model represents the state (data) and business logic of the application.It contains business and persistence logic.

View The view module is responsible to display data i.e. it represents the presentation.

Controller The controller module acts as an interface between view and model. It intercepts all the requests i.e. receives input and commands to Model / View



comments powered by Disqus