The purpose of a server is to respond to client requests. In this lesson, the server is a web server, the client is the internet browser (Internet Explorer, Chrome, Firefox), and the requests are HTTP requests. The server contains files that a client can access. These files can include HTML files, images, and text documents. A client can access these documents using the HTTP (Hypertext Transfer Protocol) protocol to speak to the server and make the request. The HTTP protocol can be thought of as a set of rules that the client and server agree upon to communicate to with one another.
Once the connection between client and server is made, the server can process the client's request with a response. The server can respond in two ways, a static response or a dynamic response, depending on the request by the client. A server gives a static response if the client requests a pre-written file. Examples of these are requesting for an image file or a PDF. In a static response case, the server searches, or fetches, for the file in its harddrives or cache and sends the file to the client using the HTTP protocol. Unlike the static response, the dynamic response is "built-on-the fly." Examples of these responses are when the client requests for blogs, a Google search, and your Facebook homepage. The content of these responses are generated by programs in the server. These programs are called web applications and they generate dynamic responses which the server then sends to the client using the HTTP protocol.
For the client to access the files in the server, the client must make a HTTP request. An HTTP request is a message that the client sends to the server. When you enter the URL (Uniform Resource Locator) in the address bar of your web browser, whether by typing it in or clicking on a link, a HTTP request is sent to the server (host) that is specified in the URL. The URL is composed of three parts: the protocol, the host, and the path. The protocol is HTTP for this lesson. The host is the domain name (server name) that the client wants to have access to. The path is the document that is being requested from the server. From this, we see that entering the URL sends a HTTP request to the server.
The first line of the HTTP request is the request line. The request line is divided in three parts: the method, the path, and the version. The method specifies what action the server needs to take. The most common are the GET method and the POST method. GET is used to fetch data on the server and POST is used for updating data on the server. The path is the document that is being requested from the server. The version is the HTTP protocol version that is being used to communicate. Following the request line are headers. These include the host, which is the domain name (server name) that has the document the client wants to access, and the User-Agent, which describes who or what browser is making the request. Knowing the parts of a HTTP request, we can see that a server uses the method portion of the HTTP request to know what action to take, and uses the path to know on what document to take the specified action on.
After the HTTP request is processed by the server, it sends a HTTP response back to the client along with the document or document information that was requested by the client. HTTP responses are in a similar format as HTTP requests. The first line of a HTTP response is the status line. The status line is composed of the version, status code, and the reason phrase. The version, for this lesson, is the HTTP version. The status code is a numerical representation of the status of the response. A status code of 200 means that the request was ok while a status code of 404 means that the document or document information was not found. The reason phrase is the English language description of the status code. The status line is followed by headers such as the Date header, which is the date and time the response was sent, and the Content-Type, which is the type of document to be returned. The server sends the HTTP response to let the client know if the request was successful or if there were errors in processing the request.
Inputs from the client, whether valid, garbage, or malicious, goes to the server. Because servers can't tell the difference between good or bad data, data must undergo validation. Validation means verifying on the server side that what is being received is what was expected to be received. A month value such as February may be expected, but instead the server gets some random value like 2342 or even malicious code. The server needs to know what to do when it receives garbage or dangerous data and allow valid data to be accepted.
Validation allows a website to be secure and be user friendly. Secure means secure for the server and secure for the users. Validation lets the right people have the right access to the right information. Common forms of validation are passwords and answering questions. Validation also prevents attacks, disruption to servers, and unwanted webpage modifications that are caused by malicious or even garbage data. Validation checks to see if the input is proper and within acceptable parameters. We've seen in our lesson that data entered with HTML code modified our web page. This was fixed by adding HTML escaping, a form of validation, into our code. A lack of security or having webpages with unwanted modifications are a few examples of having a lack of validation which can deteriorate from the user experience. Data received needs to be validated to prevent attacks from hackers, work efficiently, and enhance the user experience.
As stated in Section 3, code repetition is always avoided in programming. We seen also in Section 3 that if we do not avoid repetition, production costs increases, production speed decreases, and code is bigger that it needs to be. HTML templates also avoid code repetition. In addition to avoiding code repetition, HTML templates help organize code. This organization of code make it easier to read and debug code.
HTML templates help avoid and organize code through template inheritance.
In many websites, almost all the webpages look similar and organized in the same fashion. We can write the same code over and over again to produce each page, but this is a time consuming and ineffective method. We can instead use templates. One template, a base template, can contain the content that is used in each webpage. We then use other templates, or sub templates, that contain the content that is different for each page. Then code can be written where these sub templates then inherit the repeated content from the base template in each page. Using template inheritance, we can avoid repeated code, create pages faster, and save much time.