Developing E-Commerce Applications
In Chapter 17, "Architecting Java Web Applications," you learned that there are two design models you can adopt to build Java web applications with servlets and JSP. The first model, simply called Model 1, is a page-centric model in which an application consists of a series of JSP pages. In this model, a JSP page calls another JSP page. Developing applications based on this model is very easy; however, for complex applications, this model presents maintenance nightmares. Therefore, this model is suitable only for small applications that will never grow in complexity.
The second model, Model 2, is a Model-View-Controller (MVC) architecture that separates content generation and content presentation. A Model 2 architecture is indicated by the presence of a controller servlet between the client browser and the JSP pages or servlet content that presents the content. The controller servlet dispatches HTTP requests to the corresponding presentation JSP pages based on the request URL, input parameters, and application state. In this model, presentation parts (JSP pages or servlets) are isolated from each other.
Model 2 applications are more flexible and easier to maintain and extend because views do not reference each other directly. The Model 2 controller servlet provides a single point of control for security and logging, and often encapsulates incoming data into a form usable by the back-end MVC model.
Model 2 architecture is recommended for complex applications. Building an application using this model is not as simple as using Model 1; however, this model offers many advantages over the first model. In this chapter, you see how this model is used in an e-commerce application that implements an online store.
The project is called Burnaby (named after the city east of Vancouver, British Columbia), and it is an online store that sells foods in many different categories. Here you can find chocolate, biscuits, milk, cheese, and other things you normally might find in a superstore. The project's sole purpose is to demonstrate how Model 2 applications can be designed and developed. For brevity and clarity, little or no error handling has been included and no optimization is used. The graphic design also is kept simple. A Microsoft Access database accompanies this project and is ready to use if you happen to be using Windows. Otherwise, you can build your own database based on the database structure given in the section, "The Database Structure," with any favorite database server, as long as there is a JDBC driver for that database server.