O'Reilly logo

Head First Servlets and JSP, 2nd Edition by Bert Bates, Bryan Basham, Kathy Sierra

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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), ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required