26 MVC Architecture for Web Applications

Pravin Jain

epgp books

 

Introduction

 

Welcome to this module on MVC Architecture for Web Applications. In this module, we will try to understand how to design and develop a web application, which is going to be more maintainable, which is more understandable, which is easier to create and maintain. What is this MVC.

 

MVC stands for Model-View-Controller. This is a design pattern which was first used for GUI components. In java we have seen Swing components, they are also based on this MVC design pattern. What do we mean by MVC. Let us try see this in a simple component like swing components. What is meant by MVC, MVC it says Model View and Controller, so what is model, what is view and what is controller. It simply specifies that we should have these three things which are always there in any kind of a component. Anything we develop has all these three parts. So the idea here is, to have them in separate parts rather than have them all in single place. Each responsibility, model part handled by one kind of component, we have another kind of component to handle the view part another kind of component ot handle the controller part. Everything should have all three aspects.

 

MVC in Swing Components 

 

If we consider any swing component, may be consider a simple swing component like JTextField, in a JTextField what do we have. There is some text data which is associated with it. Model is nothing but this data aspect. The data which is to be rendered. The data which is to be displayed. It is a String basically, a String which is for my particular Component. My GUI component is displaying a String. But this GUI component is setting up the pixels of my screen. it is using particular amount of recttangular area, it is showing my String in a particular way. This is what is the responsibilty of a view. The view uses the model to know what is the String to be rendered, what is the font which is to be used. Font is the part of the model, name of the font, but how it is to be rendered. How to do the rendering is the responsibility of the View.. Model is the data, View is, how to render, how to display, present part, and Controller is the interaction aspect like someone presses the key, what should be done, if someone is pressing the key at the right place, the right kind of keys, I have to update the string. There has to be someone to find what the keys being pressed, for the pressed, what is the current caret position, So, the Controller handles the interaction from the outside world. Any interaction to his component from outside is handled by the Controller.

 

That way you develop a system. Any system would have associated data, which we call as model part, there is an aspect of viewing, someone who is responsible for rendering it, showing it, so there has to be a separate component, who are responsible for how to show it. and there has to be another kind of component who know how to handle the interaction. If there is any interaction, how will it be handled. We have seen this being done in the swing components. Swing components have been following the MVC design pattern. This kind of design pattern is used very commonly in case of web application.

 

MVC for web application 

 

For any web application we develop, we are dealing with some data. There has to be data to be displayed, who will display it, what is the technology used to display it. Normally, what is being used for display. In case of web application client is a browser and the most common thing for the browser is the html. So, output is going to be html. If we have to give html output what would be the component used from your web application. In whatever we have seen so far in that it is going to be a JSP. JSP will be used as a view aspect. So View is handled by JSP. When we follow MVC for a web applications, View would be handled by JSP.

 

Model, as far as Model is concerned, it totally depends upon your web application. Whatever applications we are developing, they have their own requirements, they have their own kind of components.

 

We have various options in our applications, say, someone clicks, when someone clicks, there is some kind of an interaction with the application.

 

So, we render some JSP, on the JSP there might be some kind of Buttons and various kinds ofr options by which a user is able to interact with our application. So, whenever he presses any Button, or any other interactive component of our JSP page, What should he be interacting with. Because this is within a browser it should result in generation of a request. So, request normally would involve, when they reach the server side from the client, would involve some kind of processing to be done. So you receive a request, do some processing get some results and display it. This is the common sequence we follow. And therefore the things which is used for handling the request is Servlet. It is more appropriate, because that’s where we have the java code. It is not about rendering, it is about processing. So that can be done from a Java program, but as far as rendering is concerned, who knows more about how show a particular thing in a browser. It is the web page designers job. So, thats the job a JSP. So here we got three things again, we got Model, View, Controller.

 

Model are nothing but your various kind of data objects, which are simply having some data within them, and these data objects are nothing but simple Java Bean objects which have got the getter setter methods for various properties. We know about properties in Java Beans, properties because of get and set methods. There can be some properties which will be read-only properties because they don’t have the set method they only have the get method. So, those properties can be read-only properties. But because they have a get method of a particular kind, we consider it as a property, but it is a read-only property. So, the components can have atleast the getter methods for the properties we have, or sometimes get and set both methods. Your web application will be using such Java Beans components. It is not only that we will have only Java Beans kinds of components, we will have other components which the business logic in them. They have some kind of processing which have to be done. And then you will have, whatever data comes from the client as part of the request, what you get is normally some parameters, we use them, do some processing, after processing get the results, and these results are in the form of Java Beans components, These components, you would normally set them as part of request attribute. You put them as attribute in request, you may put them as attribute in session also. Many times in session or some of the things in request, and then pass it on, simply forward it to a JSP, for rendering purpose. But initially, whenever some request comes, niitially all request, if we are following the MVC pattern, we develop Servlets for handling the request. So, the role of controller is normally taken care by the Servlets.

 

To understand the MVC pattern, we will be having an example later on. In the next module we are going to be developing a simple web application, it is not a full-fledged web application we want to develop here, but we would like to have some kind of a web application. Some template for a web application. In any application what we will have, may be we provide for some kind of a login. Once a user is logged in, you will provide so many other things like personal information like full name etc.. You would like to render this information on subsequent pages, and then once he has finished with all the things then he might logout.

 

So we will have a login, what we will try to do in our sample application is, we will like to have a login a logout. A simple login and logout, nothing much, because that is something commonly used across all applications. And once he is logged we will have some kind of a home page where he lands and on the home page we can have a logout option.

 

So initially we have a login. Let us try to use all kinds of things which we have learnt so far. We have learnt about the cookies, we have learnt about session. So normally, where do we use cookies, how do we use cookies. Cookies could be used storing some information on the client’s machine, eg. It could be used  so that we can have kind for an automatic login.

 

To have that automatic login, if someone comes to our server and if it is found that this person already logged in earlier and there are some cookies for him, then he need not login, we can directly take him to the home page and this is done by using cookies. But whenever there is login we create a new session, we would like to start tracking him and if there is a shopping cart application, if he selects something, he wants to add something to the cart, we will have the use of session. In the session there might be a list, whatever is the selected list it can simply be added.

 

So we would like to create a sample web application which has only two pages login page and the home page. and on the home page there will be logout button. So when he does logout he is back on the login page. This is just to understand the concept of MVC architect.

 

View – We can see only two kind of screen login page asking for username and password and a checkbox, whether he would like to remember the password and the home page to welcome the user with some welcome message, where we can show him his full name and a logout button. So two JSPs for this much lets call them as login.jsp and home.jsp.

 

Consider they are some kind of interactions, By default it goes for index.jsp. We have mechanism to specify what we want as a welcome page. So, we can have Servlet being used as a welcome. We have an option called <welcome-file> in the web.xml file, where <welcome-file-list>, and in the <welcome- file>, you may mention url pattern, which would enable us to have Servlet which will be invoked whenever someone lands on the root of our application.

 

So if he specifies the root of our application we can have our Servlet to be invoked, and lets call this servlet as our welcome servlet. The role of this servlet will be to check this request which has come, if it has got cookies. If it has got cookies then lets check the cookies. extract the cookies, do we have the cookie with the username and the password. If we have the cookie with the login name and the password then we would like to use them and authenticate that and if he gets authenticated we can directly forward him to the home jsp.

 

If he is not authenticated, or if there are no such cookies, then direct him to the login.jsp. So that processing can be done by the WelcomeServlet. When the user submits the login.jsp he will be submitting the parameters for login id, and password, so when he submits this we will have a servlet to handle this also and call that servlet as the LoginServlet. The LoginServlet will be used to authenticate the user.. Lets have another class for the business logic of authentication say Authenticator.

 

We can decide to use database or not based on the requirement. Authenticator have the ability to say whether the login id and password is valid or not. If it is valid then it should give all the information about the user.

 

There can be a UserInfo data object giving the information about the user say address, email id, etc. Let us have another class say UserInfo which is a simple Java Bean component with some properties. This goes as a part of model.

 

Controller – WelcomeServlet, LoginServlet. Login will also use Authenticator and fetch the user info fetch the data object and starts tracking, creating session. So all the subsequent pages whenever they are shown to this particular user who has logged in we have the user information object.

 

In the home page there will be a welcome message with his full name and may be other information also. We might have a simple home.jsp by using a login. This home page also have logout button and if someone clicks logout button it should go to another controller for another interaction. So for handling that logout we will have LogoutServlet. Logout should simply close the session.

 

Whenever the session gets closed, it gets closed either by clicking logout button or the session gets expired there should be some clean up to be done, If any such thing is to be done we can have a listener which is our HttpSessionListener. So this components is also to be created here.

 

Servlet as a Controller. Simple java components as a model. JSPs as a view. We will need one more component in case if authentication fails we need to display some message. We can have MessageBean class to show the message if login fails.

you can view video on MVC Architecture for Web Applications
Suggested Reading:
  1. Expert One-on-One J2EE Development without EJB by Rod Johnson and Juergen Hoeller, Wrox publication
  2. Core Servlets and Java Server Pages Volume 2 by Marty Hall & Larry Brown, Second Edition, Pearson Education.
  3. Inside Servlets by Dustin R Callaway, Pearson Education.
  4. Java Server Programming for Professionals by Ivan Bayross, Sharanam Shah, Cynhthia Bayross and Vaishali Shah Shroff Publishers and Distributros.