- Client server communication takes place in a specific pattern which is known as application design pattern or architecture. The application is developed according to different modules specified in architecture. Different types of architecture can be:
MVC Architecture, Visitor architecture, state architecture, observer architecture etc.
- J2EE (i.e. Enterprise Java applications such as Spring, Struts, Hibernate, JDBC integrated framework) follow MVC architecture with different tier models. MVC is implemented in J2EE using following models:
- Single-tie or one-tier architecture
- Two-tier architecture
- Three-tier architecture
- N-tier architecture
- MVC is an abbreviation for Model-View-Controller.
- For ease of development and deployment of the project, the web-application is divided into basic 3 modules: model, view and controller.
- As the name suggests:
- Model: responsible for handling database interaction with the controller (database layer)
- View: responsible for displaying contents of user (presentation layer)
- Controller: responsible for acting as per user action (business logic layer)
- Considering the example of AWT or Java Swing (Drop-down menu/Combobox menu), if the user wants to retrieve the states belonging to the selected country from drop-down menu,
- Model: retrieves the selected names of states from the database
- View: displays the Combobox menu to the user (coordinates with model)
- Controller: sends message to model about the user’s selection of country name.
- Considering Struts or Spring or Hibernate frameworks:
- Model: Java Beans (database)
- View: HTML or JSP page
- Controller: back-end servlet (converted JSP’s)
- The complete architecture is mainly controlled by the controller. The end-user (client) actions on view, based on which the controller delegates the requests to model. The model retrieves required data from beans or database (through database queries) and responses back to the controller. Based on the response received, the controller changes/modifies the data in view.
- The figure below represents MVC in client-server:
Practical Scenario: how to use MVC
- View : It is the presentation layer where all the components (buttons, checkboxes, radiobuttons, textfields, textarea etc) are placed and arranged in a container.
- The client-server architecture in which every communication element (such as presentation layer, business logic and database layer) is placed in a single unit is termed as one-tier architecture or single-tier architecture.
- For example:
- Every unit responsible for client-server interaction is located in one place in the client. The same architecture can be repeated for the server as well as n-number of clients.
- This type of arrangement is considered to be the simplest and most direct arrangement. The ease-of-access to database layer as well as business application layer increases.
- The clients or servers following this architecture are also known as thick clients or servers.
- Better performance: as these layers are inbuilt, there is no issue regarding lack of access or loss of connection etc.
- More security: database comes embedded in this architecture, i.e., the client who wants to access data and business logic has full rights.
- Improved scalability: Every application module is closely connected; hence they can be scaled easily.
- As all the three elements are installed on every client, the cost of developing this architecture increases.
- Maintenance cost also increases.
- In two-tier architecture, the GUI (view module) is located at client’s side while the business logic (controller) and database (model) are located at server side.
- The client sends request to server through view module and the application at server side directly communicates with the database and retrieved results are sent back to the client.
The communication between client-server requires constant internet or intranet connection.
- To understand more, let’s take an example of online movie ticket booking system. Consider Mr. ABC is reserving tickets for seats A1 and A2. At the same time Mr. XYZ is also reserving tickets for the same day. In this case, the requests coming from both the clients are in queue on the server. If the request coming from client 1 is not responded and simultaneously, request from client 2 is served, the data in the database can be left in an inconsistent state. This may cause data integrity issues.
- Faster Communication
- As only the GUI is to be maintained at client side, the database maintenance and modification becomes easy.
- Less cost in developing
- Constant internet or intranet connection is required
- As the number of clients increases, the server may lag in processing client’s requests.
MVC for movie ticket booking system:
- In three-tier architecture, two servers and one client are involved.
- MVC is divided as:
- Model : Database Server
- View : Client Machine
- Controller : Application Server
- The client communicates with application server in the form of a request. These requests are served by the application server in form of responses. The responses include retrieval of data from database by the application server.
- The architecture is:
- More security: as the client has no communication with the database, it is more secure as compared to one-tier architecture.
- Modification of individual modules is possible (for example, modification in the database is possible without affecting other modules).
- If one of the tier stops working, the other modules can still function properly.
- More scalable as compared to one-tier and two-tier architecture.
- Increased in complexity
- Maintenance cost of all the modules increases
- The separation may lead to performance issues.
MVC in Struts:
- MVC in struts can be differentiated as:
- Model: Java Beans (that interact with database at back-end)
- View: JSP (Java + HTML)
- Controller: Servlet (or filters)