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.

image with no caption

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), your web app really COULD have more than one ServletContext. A ServletContext is one per app, but only if the app is in a single JVM!

In a distributed environment, you’ll have one ServletContext per JVM. Now, chances are this won’t create problems, but if you have a distributed web app, you better consider the consequences of having different contexts for each JVM.

Think of init parameters as deploy-time constants!

You can get them at runtime, but you can’t set them. There’s no setInitParameter().

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset