Part 1
Chapter 1: The Invisible River...--Every time you check your email, scroll through social media, or buy something online, you are riding an invisible river. This river has no current you can feel, no surface you can see, but it carries trillions of messages every single day across continents, under oceans, and through the air. Its name is HTTP—Hypertext Transfer Protocol—and understanding how it works is the difference between someone who merely uses the web and someone who builds the web.If you are reading this book, you want to be a builder. Specifically, you want to become a Java Web Developer. That means you will write code that sits on servers, waits for requests to arrive, and sends back responses that become web pages, API data, or file downloads.
But before you write a single line of Java, you need to understand the conversation that happens between a browser and a server. You need to understand the invisible river.This chapter is the foundation upon which every other chapter in this book is built. We will start with the absolute basics: what happens when you type https://www.google.com into a browser and press Enter. Then we will dissect the HTTP protocol—its methods, status codes, and headers—until you can read raw HTTP traffic like a fluent speaker of a foreign language. You will install Apache Tomcat, the most popular Java servlet container, and deploy your first web application. And you will use your browser's developer tools to spy on the conversation between your computer and a server.By the end of this chapter, you will never look at a web page the same way again.
Behind every button, every image, every form submission, you will see the invisible river flowing. And you will be ready to start building boats to sail on it.The Journey of a Single ClickLet us begin with a thought experiment. You wake up in the morning, open your laptop, and type https://www.example.com/products?id=42 into your browser's address bar. You press Enter. What happens next?Most people think: "The web page appears." But between that keystroke and the rendered page, an astonishing sequence of events unfolds in milliseconds. Here is the high-level journey:Your browser extracts the domain name from the URL: www.example.com.It performs a DNS lookup to convert that human-readable domain into an IP address, something like 93.184.216.34.It opens a TCP connection to that IP address on port 443 (for HTTPS) or port 80 (for HTTP).It constructs an HTTP request containing the path /products?id=42, the method GET, and various headers.It sends that request across the internet, hopping through routers and switches, until it reaches the server at that IP address.The server processes the request—looking up product 42 from a database, perhaps—and constructs an HTTP response.The server sends the response back across the internet to your browser.Your browser parses the response, which might be HTML, and begins rendering the page, making additional requests for images, CSS files, and JavaScript as it goes.This entire sequence happens in less than a second for a well-performing website.