Web App Architecture
Servlet · Container · MVC · Deployment Descriptor
🗂
Key Topics to Know
Web App Architecture — How Servlet works inside a Container
The Container — What it provides & why we use it instead of plain Java
Request Handling Lifecycle — How Request/Response objects are created & managed
Deployment Descriptor (DD) — Using web.xml to map URLs to Servlets
MVC Pattern — Separating Business Logic from Presentation
📖
Core Definitions
Container
A Java app (e.g. Tomcat) that controls the Servlet. Loads classes, creates objects, calls doGet() / doPost(), and manages resources.
Servlet
A Java class with NO main() method. Runs under Container control to generate dynamic content in response to client requests.
DD — web.xml
An XML config file. Maps URLs to Servlets & handles security. No recompilation needed when changed!
MVC Pattern
Model (data & logic) + View (JSP) + Controller (Servlet routing). Keeps code clean & reusable.
📦
What the Container Provides — 5 Benefits
1
Communications Support
No Socket code needed. Container handles all web server communication automatically.
2
Lifecycle Management
Controls Servlet birth & death: loading → instantiation → destruction → GC.
3
Multithreading Support
New Thread created automatically per request. You don't manage threads!
4
JSP Support
Container translates JSP → real Java. 100% the Container's responsibility.
5
Declarative Security
Manage security via XML config. No complex security code inside the Servlet.
⚠️
Tomcat Note
Web Container only — NOT a full J2EE server. No EJB Container!
Quick Facts — Memorize!
Servlet has NO main() method
Container creates HttpServletRequest & HttpServletResponse
Container calls service() → decides doGet() or doPost() based on HTTP method
When request ends → Thread dies + Request/Response objects are destroyed
// Request flow
Client → HTTP Request → Container → creates Thread → creates Request/Response → calls service()doGet() or doPost() → Response sent → Thread killed
🏷
A Servlet Has 3 Names
#Name TypeExample
1Real file pathclasses/registration/SignUpServlet.class
2Internal deploy name (DD)EnrollServlet
3Public URL name/registerMe
// Why mapping?
Mapping in the DD gives you flexibility to rename files or restructure without breaking client URLs. It also hides the real file structure from the public for added security.
🔺
MVC Design Pattern
Controller
Servlet
Receives client request. Talks to Model. Decides which View to show.
Model
Plain Java Class
Business Logic & Data. Knows NOTHING about the web or presentation.
View
JSP Page
Presentation only. Takes data from Model and displays it to user.
// Why use MVC? Putting Business Logic inside the Servlet = hard to maintain & can't reuse in mobile or Swing apps. MVC keeps things clean, separated, and reusable.