Database and Web Fundamentals· 30 min

HTTP, REST, and Servlets

Understand HTTP semantics, REST constraints, status codes, idempotency, and the servlet request lifecycle.

Java interview lesson
Question 14 · Not reviewed

Web fundamentals · protocols

Can you explain this clearly in an interview?

Use the strong answer as your reference, explain it aloud in your own words, then choose how soon you want to review it again.

Interview prompt

HTTP, REST, and Servlets

Explain it aloud in your own words

Strong answer

HTTP is a request-response protocol with methods, status codes, headers, and representations. REST APIs should make resource semantics and idempotency clear. A servlet container manages the lifecycle around request handling; Spring MVC builds higher-level abstractions over the same web fundamentals.

Key points

  • GET should be safe; PUT and DELETE are intended to be idempotent.
  • Use status codes to communicate outcome, not just a JSON error string.
  • Authentication and authorization are separate decisions.
  • Pagination, validation, and consistent errors are part of an API contract.

Interviewer looks for

Describe the HTTP behavior a client can rely on, including retries, caching, errors, and idempotency.

Java example

1GET /api/users?page=0&size=20
2
3HTTP/1.1 200 OK
4Content-Type: application/json
5
6{"items": [], "page": 0, "size": 20}

Topic interview questions

Revise this topic like an interviewer

These questions are tied to this lesson. Read each question aloud, then compare your explanation with the strong answer.

5 questions
Q1Intermediate

What does idempotent mean in HTTP?

Strong answer

An operation is idempotent when repeating the same request has the same intended server-side effect as making it once. PUT and DELETE are designed to be idempotent, while POST is not inherently so.

Q2Intermediate

Which HTTP status codes should an API commonly use?

Strong answer

Use 2xx for success, 3xx for redirects or cache revalidation, 4xx for client or authorization problems, and 5xx for server-side failures. The body should provide stable, useful error details without leaking internals.

Q3Intermediate

What is the servlet lifecycle?

Strong answer

The container loads and instantiates a servlet, calls init, invokes service for requests, and eventually calls destroy. A servlet instance may handle concurrent requests, so mutable instance fields need careful synchronization.

Q4Intermediate

What makes a REST endpoint practical for clients?

Strong answer

Stable resource semantics, clear status codes, validation, authentication rules, pagination, consistent errors, caching behavior, and documented idempotency make an API predictable and safe to integrate.

Q5Intermediate

Why should GET be safe?

Strong answer

GET is intended to retrieve a representation without changing server state, which allows browsers, crawlers, caches, and clients to retry it more freely. Side effects on GET create surprising and unsafe behavior.

How well do you know this?

Your review choice is saved locally and synced to your learner profile when signed in.