Nearby lessons
8 of 34Servlet - HTTP Methods (GET and POST)
- Understand HTTP request and response structure
- Learn GET vs POST differences
- Understand idempotent and safe requests
HTTP Methods define how the web client communicates with the web server. This lesson covers HTTP request/response structure, GET vs POST methods, and idempotent/safe request concepts.
HTTP Request Structure
Web client and web server communicate using HTTP. HTTP defines standard structures for request and response.
Structure of HTTP Request:
- Request Line — Contains method, resource, and protocol version
- Request Headers — Configuration info about the browser (media types, languages, encoding)
- Request Body — End-user provided information (resume, username, etc.)
Request Line example: GET /advapps1A/test HTTP/1.1
HTTP Response Structure
Structure of HTTP Response:
- Status Line — Protocol version, status code, and description
- Response Headers — Server config info (content type, content length, last modified date)
- Response Body — Original response provided by server, displayed to end user
Status Code Ranges:
| Range | Category |
|---|---|
| 1XX | Informational |
| 2XX | Successful |
| 3XX | Redirection |
| 4XX | Client Error |
| 5XX | Server Error |
Note: Request headers are sent by browser, used by server. Response headers are sent by server, used by browser.
Types of HTTP Methods
Big-7 HTTP Methods (introduced in HTTP/1.0):
- GET
- POST
- HEAD
- PUT
- DELETE
- OPTIONS
- TRACE
Additional methods (introduced in HTTP/1.1):
- CONNECT
- MOVE
- LOCK
- ... and others
Until Servlet 2.4, web servers supported only the first 7 methods. From Servlet 2.5 onwards, all methods are supported.
Note: As a Java developer, you need to be aware of GET and POST methods primarily.
GET Method
Use GET when you want to get information from the server. GET requests are usually read-only.
| Feature | GET |
|---|---|
| Purpose | Get information from the server |
| Read/Write | Usually read-only |
| Data transmission | Appended to URL as query string: test?uname=durga&pwd=java |
| Data visibility | Visible in URL — not secure for sensitive information |
| Data size | Limited amount (varies by browser) |
| Data type | Character data only (no binary like images, video) |
| Bookmarking | Possible |
| Caching | Possible — improves performance |
| Idempotent | Yes — repeating doesn't change the response |
| Safe | Yes — no side effect on server |
| Body | Optional |
Triggers for GET Request:
- Typing URL in address bar and pressing Enter
- Clicking a hyperlink
- Submitting form without method attribute (default is GET)
- Submitting form with
method="GET"
POST Method
Use POST to send information to the server. POST requests are usually write/update operations.
| Feature | POST |
|---|---|
| Purpose | Send information to the server |
| Read/Write | Usually write or update operations |
| Data transmission | Encapsulated in request body — not visible in URL |
| Data visibility | Not visible in URL — secure for sensitive information |
| Data size | No limit — can send huge amounts of data |
| Data type | Both character and binary data |
| Bookmarking | Not possible |
| Caching | Not applicable |
| Idempotent | No — repeating may change server state |
| Safe | No — has side effects on server |
| Body | Mandatory |
Trigger for POST Request: Submitting form with method="POST"
GET vs POST Comparison
Complete comparison table:
Idempotent and Safe Requests
Idempotent Request: By repeating the request multiple times, if there is no change in the response, such requests are called idempotent. Example: GET is idempotent, but POST is not.
Safe Request: By repeating the request several times, if there is no side effect on the server side, such requests are called safe. Example: GET is safe, but POST is not.
- Request Line, Headers, and Body structure
- GET: read-only, URL-appended, limited, bookmarkable
- POST: write/update, body-encapsulated, unlimited, not bookmarkable
- Exam-style questions at the end