ServletConfig is one per servlet ServletContext is one per web app
There’s only one ServletContext for an entire web app, and all the parts of the web app share it. But each servlet in the app has its own ServletConfig. The Container makes a ServletContext when a web app is deployed, and makes the context available to each Servlet and JSP (which becomes a servlet) in the web app.
Web app initialization:
Container reads the DD and creates a name/value String pair for each <context-param>.
Container creates a new instance of ServletContext.
Container gives the ServletContext a reference to each name/value pair of the context init parameters.
Every servlet and JSP deployed as part of a single web app has access to that same ServletContext.
Watch it!
Don’t confuse ServletConfig parameters with ServletContext parameters!
You really have to keep these straight on the exam, and it’s tricky. You MUST know that both ServletConfig and ServletContext have init parameters, and both have the same getter method—getInitParameter(). BUT... you also have to know that context init parameters are set with <context-param> (not inside a <servlet> element) while servlet init parameters use <init-param> inside the individual <servlet> declarations in the DD.
Watch it!
If the app is distributed, there’s one ServletContext per JVM!
If your application is distributed across multiple servers (probably in a clustered environment), ...
Get Head First Servlets and JSP, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.