In this lesson, we will apply layers in the architecture of web based systems. Previously, we defined a layer as a collection of components that work together toward a common purpose. We use layers to identify hierarchies in the system and to restrict how layers interact with each other. Components in a layer only interact with components in their own layer or adjacent layers and they may do so only through interfaces provided by each component. Generally, lower layers provide services to the layers above them. More complex systems often require more layers to help logically separate the components. The downside of adding additional layers is that performance suffers due to the increase in communication required between the layers. Often, layered systems are conceptually organized into presentation, application, and data tiers. In a web-based system, we divide the presentation tier into two layers. One for the web browser and one for the web server. Let's take a look at the function of each layer in a web-based system. We'll depict the architecture using a UML deployment diagram, with each layer supported by a node. The web browser layer is the top-most layer of the system. It displays the information to the user. The web server layer sits directly below the web browser layer. It receives a request from the web browser, obtains the requested content and then returns it to the browser. Below this, is the application layer which is responsible for ensuring the function or service provided by the system is performed. The bottom layer is the data layer. This layer is responsible for storing, maintaining, and managing data. Access to data maybe read-only or may allow for both reading and writing. Depending on the system, this can take the form of a file system or a database. These are the typical layers that web systems are broken into but by no means is this the only way to separate a web-based system into layers. As we shall soon see, not all systems require all four layers and not all systems use these layers in the same way. Let's begin by looking at how a layered architecture can be applied to deliver static web content. To present a static web page, a layered architecture requires a web browser layer, web server layer and a data layer. The web browser layer consists of the web browser which displays the information provided by the web server. The web server layer receives the request from the web browser and grabs the appropriate HTML documents stored in the data layer. After it has accessed, it returns the requested content to the browser. The data layer consists of static HTML documents that will be delivered back to the web browser unchanged by the web server. Since the HTML documents do not change, the file system can be read-only. There is no application layer present in the system because the HTML documents served by the web server are the exact same HTML documents stored in the file system. No processing is necessary. How could a layered architecture be applied to delivering dynamic web content? Let's take a look. To present a dynamic web page, a layered architecture requires a web browser layer, web server layer, application layer, and data layer. The web browser and web server presentation perform in the same way as with static content. Unlike a static web page however, HTML documents are generated when they are requested by a web browser. The web server passes on the request to an application server in the application layer for processing. For dynamic web pages, the application layer may consist of one or more programs or applications that process the request to generate the resulting content. The application layer may also call upon other web services and read and write data to a database via the data layer. This layering scheme also applies to architectures for complex web applications. As an alternative view of the architecture, we can use a UML component diagram and think of the design of a web-based system as a collection of services and service requester provider pairings. For example, the database provides data services and the application server is a service requester to the database. The application server runs programs that may access a variety of web services provided outside the system. Those web services may themselves access other services. In this lesson, we have reviewed layered architectural style and learned how to apply it to deliver both static and dynamic web content. Web-based systems that present static content can be mapped to a data layer and two presentation layers. Web-based systems that present dynamic content require an application layer in between the data layer and the web server layer to generate custom HTML documents when requested. Dynamic web systems may run programs in the application layer and call upon web services. In a way, the layered architecture and use of outside web services has taken us back to very basic design principles, Separation of Concerns and Code Reuse. The layers have very specific responsibilities. The use of outside services provides functionality that the system does not have to implement. A great example of software reuse. The scope of using web services is huge, but it does pose a challenge of identifying the right ones to use. To make this all work, especially in the face of different platforms or languages, it's important to have standards for web content and communications. We will cover these in upcoming lessons.