Why HTTP Matters: From a Simple Story to Real‑World Web Requests
This article explains the purpose and structure of the HTTP protocol, using a humorous story and Java code examples to illustrate request‑response mechanics, URL syntax, headers, and servlet interfaces, helping developers grasp how browsers and servers communicate over the web.
Introduction
HTTP is the foundation of web development; most people know it only superficially, but understanding details such as how sessions rely on the Cookie header becomes clear once the protocol is examined.
Story Illustration
A short tale about two developers, A and B, who are asked to create a StringUtils utility class with isNull and isEmpty methods, then use it in a card‑number validator and a client program.
public abstract class StringUtils {
public static boolean isNull(String s) {
return s == null;
}
public static boolean isEmpty(String s) {
return isNull(s) || s.length() == 0;
}
}
public class CardNumberValidator {
public boolean valid(String cardNumber) {
if (StringUtils.isNull(cardNumber)) {
throw new NullPointerException("cardNumber is null!");
} else if (StringUtils.isEmpty(cardNumber)) {
throw new IllegalArgumentException("cardNumber is empty!");
} else if (cardNumber.length() == 18) {
return true;
} else {
throw new IllegalArgumentException("the length of cardNumber must be 18!");
}
}
}
public class Client {
public static void main(String[] args) {
CardNumberValidator validator = new CardNumberValidator();
String cardNumber = "433182198803211232";
if (validator.valid(cardNumber)) {
System.out.println(cardNumber + "是一个有效的身份证号!");
} else {
System.out.println(cardNumber + "是一个无效的身份证号!");
}
}
}The program runs successfully and prints that the sample ID number is valid.
Analogy to a Web Request
The three steps of the story—locating a method, passing parameters, and receiving a result—mirror a browser’s request to a server: locating a resource via URL, sending a request with headers and optional body, and receiving a response with status, headers, and body.
URL Structure
A URL follows the pattern
protocol://hostname[:port]/path/[;parameters][?query]#fragment, where the protocol (e.g., http, https) identifies the application‑layer protocol.
HTTP Request Example
GET http://www.cnblogs.com/mvc/Follow/GetFollowStatus.aspx HTTP/1.1
Host: www.cnblogs.com
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0
Accept: text/plain, */*; q=0.01
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
X-Requested-With: XMLHttpRequest
Referer: http://www.cnblogs.com
Cookie: _ga=...; .CNBlogsCookie=...; ...
Connection: keep-aliveThe first line is the request line; the following lines are headers that control server behavior.
HTTP Response Example
HTTP/1.1 200 OK
Date: Fri, 17 Apr 2015 10:18:06 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 128
Connection: keep-alive
Cache-Control: private
X-UA-Compatible: IE=10
<a href="javascript:void(0);" onclick="cnblogs.UserManager.FollowBlogger('...')">+加关注</a>The status line indicates success (200 OK); subsequent headers describe the payload, and the final line is the HTML body.
Servlet Interfaces
Java’s HttpServletRequest and HttpServletResponse APIs directly reflect HTTP concepts. Key methods of HttpServletRequest include getHeader, getCookies, getMethod, getRequestURI, and session‑related methods. HttpServletResponse defines status constants (e.g., SC_OK, SC_NOT_FOUND) and methods such as addCookie, setHeader, sendError, and sendRedirect.
These interfaces are updated whenever the HTTP specification evolves, ensuring that servlet code stays aligned with the protocol.
Conclusion
Understanding HTTP’s role, its request‑response flow, URL format, and header mechanics helps developers write better web applications and even extend the protocol for features like authentication or custom session handling.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
