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
JCode Cell
1 
2GET Introduced in HTTP/1.0 V
3POST
4HEAD
5 
6OPTIONS
7TRACE Introduced in HTTP/1.1 V
8CONNECT
9MOVE
10LOCK
11

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
JCode Cell
1 
2We can use GET Method to GET Information 1) We can use POST Method to POST
3from the Server. Information to the Server.
4 
52) Usually GET requests are READ-ONLY. 2) Usually POST Requests are WRITE or UPDATE
6 Operations.
7 
83) End User provided Information will be 3) End User provided Information will be
9append to the URL as the Part of Query encapsulated in the Request Body and send to
10String and send to the Server. the Server.
11 
124) By using GET Request we can send only 4) By using POST Request we can send both
13Character Data (ASCII) and we cannot send Binary and Character Data to the Server.
14Binary Data like Images.
15 
165) By using GET Request we can send only 5) By using POST Request we can send huge
17limited Amount of Information, which is Amount of Information to the Server.
18varied from Browser to Browser.
19 
206) Security is less and hence we cannot 6) Security is more and hence we can send
21send sensitive Information like User sensitive Information like User Name, Password
22Name, Password etc. etc.
23 
247) Book Marking of GET Request is 7) Book Marking of POST Request is not
25possible. possible.
26 
278) Caching of GET Request is possible. 8) Caching of POST Request is not possible.
28GET Request is Idempotent. 9) POST Request is not Idempotent.
29GET Request is safe. 10) POST Request is not safe.
30There are multiple ways to send GET 11) There is only one way to send POST
31Request: Request: Submitting the HTML FORM with
32Typing URL in the Address Bar and enter Method Attribute of POST Value.
33Clicking Hyper Link
34Submitting the HTML FORM with
35Method Attribute of GET Value.
36

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