When the Container makes a servlet, it reads the DD and creates the name/value pairs for the ServletConfig. The Container never reads the init parameters again! Once the parameters are in the ServletConfig, they won’t be read again until/unless you redeploy the servlet. Think about that.
Container reads the Deployment Descriptor for this servlet, including the servlet init parameters (<init-param>).
Container creates a new ServletConfig instance for this servlet.
Container creates a name/value pair of Strings for each servlet init parameter. Assume we have only one.
Container gives the ServletConfig references to the name/value init parameters.
Container creates a new instance of the servlet class.
Container calls the servlet’s init() method, passing in the reference to the ServletConfig.
There are no Dumb Questions
Q:
A:
A: With Tomcat, there isn’t a one-button, really simple admin tool for deployment and redeployment (although there is an admin tool that ships with Tomcat). But think about it—what’s the worst you have to do to change the servlet’s init parameters? You make a quick change to the web.xml file, shut down Tomcat (bin/shutdown.sh), then restart Tomcat (bin/startup.sh). On restart, Tomcat looks in its webapps directory, and deploys everything it finds there.
Q:
Q: Sure it’s easy to tell Tomcat to shutdown and startup, but what about the web apps that are running? They all have to go down!
A:
A: Technically, yes. Taking your web apps down so that you can redeploy one servlet is a little harsh, especially if you have a lot of traffic on your web site. But that’s why most of the production-quality Web Containers let you do a hot redeploy, which means that you don’t have to restart your server or take any other web apps down. In fact, Tomcat does include a manager tool that will let you deploy, undeploy, and redeploy entire web apps without restarting Tomcat. In a production environment, that’s what you’d use. But for testing, it’s easier to just restart Tomcat. Info on the management tool is at:
http://jakarta.apache.org/tomcat/tomcat-5.0-doc/manager-howto.html
But in the real world, even a hot redeploy is a Big Deal, and taking even a single app down just because the init parameter value changed can be a bad idea. If the values of your init parameters are going to change frequently, you’re better off having your servlet methods get the values from a file or database, but this approach will mean a lot more overhead each time your servlet code runs, instead of only once during initialization.