Nearby lessons

8 of 34

Servlet - HTTP Methods (GET and POST)

📌 What You Will Learn
  • 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:

  1. Request Line — Contains method, resource, and protocol version
  2. Request Headers — Configuration info about the browser (media types, languages, encoding)
  3. 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:

  1. Status Line — Protocol version, status code, and description
  2. Response Headers — Server config info (content type, content length, last modified date)
  3. Response Body — Original response provided by server, displayed to end user

Status Code Ranges:

RangeCategory
1XXInformational
2XXSuccessful
3XXRedirection
4XXClient Error
5XXServer 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.

Example03
Loading editor…

GET Method

Use GET when you want to get information from the server. GET requests are usually read-only.

FeatureGET
PurposeGet information from the server
Read/WriteUsually read-only
Data transmissionAppended to URL as query string: test?uname=durga&pwd=java
Data visibilityVisible in URL — not secure for sensitive information
Data sizeLimited amount (varies by browser)
Data typeCharacter data only (no binary like images, video)
BookmarkingPossible
CachingPossible — improves performance
IdempotentYes — repeating doesn't change the response
SafeYes — no side effect on server
BodyOptional

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.

FeaturePOST
PurposeSend information to the server
Read/WriteUsually write or update operations
Data transmissionEncapsulated in request body — not visible in URL
Data visibilityNot visible in URL — secure for sensitive information
Data sizeNo limit — can send huge amounts of data
Data typeBoth character and binary data
BookmarkingNot possible
CachingNot applicable
IdempotentNo — repeating may change server state
SafeNo — has side effects on server
BodyMandatory

Trigger for POST Request: Submitting form with method="POST"

GET vs POST Comparison

Complete comparison table:

Example06
Loading editor…

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.

📝 Key Takeaways
  • 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

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.