Tuesday, July 8, 2008

Applying Model Viewing Controller to Java Web Applications Using JSP and Servlet

Model viewing controller is an architectural pattern. We can use to separate the user interface and the business logic. By decoupling models and views, MVC helps to reduce the complexity in architectural design, and to increase flexibility and reuse.


It isn't goal of this post to explain the using JSP and Servlet technologies. Maybe in another post I will do it.


Participants of the Model Viewing Controller Design Pattern
(based on Wikipedia)

model - the domain-specific representation of the information on which the application operates. Domain logic adds meaning to raw data (e.g., calculating whether today is the user's birthday, or the totals, taxes, and shipping charges for shopping cart items). Many applications use a persistent storage mechanism (such as a database) to store data. MVC does not specifically mention the data access layer because it is understood to be underneath or encapsulated by the Model.
viewing - renders the model into a form suitable for interaction, typically a user interface element. Multiple views can exist for a single model for different purposes.
controller - processes and responds to events, typically user actions, and may invoke changes on the model.

A simple diagram depicting the relationship
between the Model, View, and Controller.


Model Viewing Controller in action
  1. The user interacts with the user interface.
  2. A controller handles the input event from the user interface, often via a registered handler or callback.
  3. The controller notifies the model of the user action, possibly resulting in a change in the model's state.
  4. A view uses the model (indirectly) to generate an appropriate user interface. The view gets its own data from the model. The model has no direct knowledge of the view.
  5. The user interface waits for further user interactions, which begins the cycle anew.



Model Viewing Controller in action


Participants of the Model Viewing Controller Design Pattern - Java Web Application Using JSP and Servlet

model
- a web application usually operates on database. There are several ways to access database from web application (JDBC, Hibernate, Toplink...).
viewing - Java Server Pages (JSP) can be used as user interface. With JSP technologies we can generate contents to web browsers, mobile phones, XML files.
controller - Servlets was developed to control the web application and access to the database, what the application use.

Model Viewing Controller in action - Java Web Application Using JSP and Servlet

In the first step, the user visits a web portal with his web browser:


Example web application

The viewed content is generated by a viewer, a JSP file:

By clicking Greeting! button, the given name will be sent to the controller Servlet. In this time the form's content is sent to the specified controller (action property, in this case greeting.controller). The property method specify the type of request (GET or POST).

The controller, in this case the GreetingServlet get the http request.
The servlets have two entry point, depends on it get a http GET or http POST query:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { ... }
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { ... }
So depend on the type of request, the suitable method is invocated, the specified codes begin to run.

In my example the servlet looks like this:

The servlet processes the data and the processing is forwarded to the result.jsp, where greetings message will be showed:

Downloads
You can download example code here as NetBeans project.

No comments: