Now that
you know the purpose and potential benefits of server-side programming we're going to examine in detail what happens when a server receives a "dynamic request" from a browser. As most website server-side code handles requests and responses in similar ways, this will help you understand what you need to do when writing most of your own code. There is no real code in the discussion because we haven't yet chosen a web framework to use to write our code! This discussion is however still very relevant, because the described behavior must be implemented by your server-side code, irrespective of which programming language or web framework you select. Web browsers communicate with web servers using the HyperText Transfer Protocol
(HTTP). When you click a link on a web page, submit a form, or run a search, the browser sends an HTTP Request to the server. This request includes: Web servers wait for client request messages, process them
when they arrive, and reply to the web browser with an HTTP Response message. The response contains an HTTP Response status code indicating whether or not the request succeeded (e.g. " When an HTML page is returned it is rendered by the web browser. As part of processing the browser may discover links to other resources (e.g. an HTML page usually references JavaScript and CSS files), and will send separate HTTP Requests to download these files. Both static and dynamic websites (discussed in the following sections) use exactly the same communication protocol/patterns. You can make a simple Note: The format of HTTP messages is defined in a "web standard" (RFC9110). You don't need to know this level of detail, but at least now you know where this all came from! Each line of the request contains information about it. The first part is called the
header, and contains useful information about the request, in the same way that an HTML head contains useful information about an HTML document (but not the actual content itself, which is in the body): The first and second lines contain most of the information we talked about above:
The final line contains information about the client-side cookies — you can see in this case the cookie includes an id for managing sessions ( The remaining lines contain information about the browser used and the sort of responses it can handle. For example, you can see here that:
HTTP requests can also have a body, but it is empty in this case. The responseThe first part of the response for this request is shown below. The header contains information like the following:
At the end of the message we see the body content — which contains the actual HTML returned by the request.
The remainder of the response header includes information about the response (e.g. when it was generated), the server, and how it expects the browser to handle the page (e.g. the POST request/response exampleAn HTTP The requestThe text below shows the HTTP
request made when a user submits new profile details on this site. The format of the request is almost the same as the
The main difference is that the URL doesn't have any parameters. As you can see, the information from the form is encoded in the body of the request (for example, the new user fullname is set using: The responseThe response from the request is shown below. The status code of "
Note: The HTTP responses and requests shown in these examples were captured using the Fiddler application, but you can get similar information using web sniffers (e.g. Websniffer) or packet analyzers like Wireshark. You can try this yourself. Use any of the linked tools, and then navigate through a site and edit profile information to see the different requests and responses. Most modern browsers also have tools that monitor network requests (for example, the Network Monitor tool in Firefox). Static sitesA static site is one that returns the same
hard coded content from the server whenever a particular resource is requested. So for example if you have a page about a product at Note: Static sites are excellent when you have a small number of pages and you want to send the same content to every user. However they can have a significant cost to maintain as the number of pages becomes larger. Let's recap on how this works, by looking again at the static site architecture diagram we looked at in the last article.
When a user wants to navigate to a page, the browser sends an HTTP The server for a static site will only ever need to process GET requests, because the server doesn't store any modifiable data. It also doesn't change its responses based on HTTP Request data (e.g. URL parameters or cookies). Understanding how static sites work is nevertheless useful when learning server-side programming, because dynamic sites handle requests for static files (CSS, JavaScript, static images, etc.) in exactly the same way. Dynamic sitesA dynamic site is one that can generate and return content based on the specific request URL and data (rather than always returning the same hard-coded file for a particular URL). Using the example of a product site, the server would store product "data" in a database rather than individual HTML files. When receiving an HTTP
Using a database allows the product information to be stored efficiently in an easily extensible, modifiable, and searchable way. Using HTML templates makes it very easy to change the HTML structure, because this only needs to be done in one place, in a single template, and not across potentially thousands of static pages. Anatomy of a dynamic requestThis section provides a step-by-step overview of the "dynamic" HTTP request and response cycle, building on what we looked at in the last article with much more detail. In order to "keep things real" we'll use the context of a sports-team manager website where a coach can select their team name and team size in an HTML form and get back a suggested "best lineup" for their next game. The diagram below shows the main elements of the "team coach" website, along with numbered labels for the sequence of operations when the coach accesses their "best team" list. The parts of the site that make it dynamic are the Web Application (this is how we will refer to the server-side code that processes HTTP requests and returns HTTP responses), the Database, which contains information about players, teams, coaches and their relationships, and the HTML Templates.
After the coach submits the form with the team name and number of players, the sequence of operations is:
An operation to update a record in the database would be handled similarly, except that like any database update, the HTTP request from the browser should be encoded as a Doing other workA Web Application's job is to receive HTTP requests and return HTTP responses. While interacting with a database to get or update information are very common tasks, the code may do other things at the same time, or not interact with a database at all. A good example of an additional task that a Web Application might perform would be sending an email to users to confirm their registration with the site. The site might also perform logging or other operations. Returning something other than HTML
Server-side website code does not have to return HTML snippets/files in the response. It can instead dynamically create and return other types of files (text, PDF, CSV, etc.) or even data (JSON, XML, etc.). The idea of returning data to a web browser so that it can dynamically update its own content (AJAX) has been around for quite a while. More recently "Single-page apps" have become popular, where the whole website is written with a single HTML file that is dynamically updated when needed. Websites created using this style of application push a lot of computational cost from the server to the web browser, and can result in websites that appear to behave a lot more like native apps (highly responsive, etc.). Web frameworks simplify server-side web programmingServer-side web frameworks make writing code to handle the operations described above much easier. One of the most important operations they perform is providing simple mechanisms to map URLs for different resources/pages to specific handler functions. This makes it easier to keep the code associated with each type of resource separate. It also has benefits in terms of maintenance, because you can change the URL used to deliver a particular feature in one place, without having to change the handler function. For example, consider the following Django (Python) code that maps two URL patterns to two view functions. The first pattern ensures that an HTTP request with a resource URL of
Note: The first parameters in the The web framework also makes it easy for a view function to fetch information from the database. The structure of our data is defined in models, which are Python classes that define the fields to be stored in the underlying database. If we have a model named Team with a field of "team_type" then we can use a simple query syntax to get back all teams that have a particular type. The example below gets a list of all teams that have the exact (case sensitive)
After the Obviously web frameworks can help you with a lot of other tasks. We discuss a lot more benefits and some popular web framework choices in the next article. SummaryAt this point you should have a good overview of the operations that server-side code has to perform, and know some of the ways in which a server-side web framework can make this easier. In a following module we'll help you choose the best Web Framework for your first site.
In this moduleWhat are the components found in a 3 tier architecture quizlet?A three-tier client/server is a type of multi-tier computing architecture in which an entire application is distributed across three different computing layers or tiers. It divides the presentation, application logic and data processing layers across client and server devices.
Which one of the following components is responsible for accepting user's input data?The CPU, or central processing unit, is the processing and control center of the computer. It controls all tasks that go on in a computer; it takes inputs and turns them into outputs. RAM (random access memory) is computer hardware that temporarily stores information when software is being used.
What technique is used by most video conferencing applications to reduce the amount of data being transmitted?Video and audio input from a device's camera and microphone are converted from an analog to a digital signal that can be transmitted over the internet. Compression software (known as codec, for compression/decompression) shrinks the data to make it smaller and faster to transfer.
Which application protocol is the most popular when used in web based client server architecture?The main protocol to focus on for building web applications are Hypertext Transfer Protocol (HTTP), which is on the application layer. Another protocol on the application layer to be aware of is the Simple Mail Transfer Protocol (SMTP) for electronic mail, e-mail for short.
|