0

I am following headfirst JSP and Servlets book these days and it says that ServletConfig object is only created once during the life cycle of a servlet before being passed down to the init method of the servlet.

I was testing out some examples given in the book to just print out the init-params and context-params defined in the DD of my webapp using the out.println method of HttpServletResponse.

What I am seeing this is if I make any change in the DD (adding or changing param names, values) and reload the URL in my chrome browser pointing to that particular servlet inside the webapp it gets updated with the newly added params. This should not be the case. I am not redeploying the servlet (by stopping and re-running the tomcat service again) which will cause the ServletConfig object to be recreated. What is happening here?

I am running this on tomcat9 over windows 8 while the book refers to tomcat5. Has there been changes since to dynamically update the ServletConfig and Context init params? I couldn't see anything indicating this on the internet.

Abhay Sharma
  • 309
  • 5
  • 16

2 Answers2

1

Look at the Tomcat logs and you 'll see the app being re-deployed.

By default changes to web.xml will trigger an application redeployment. The check for modifications happens every 10-15 seconds (I forget exactly how often).

Mark Thomas
  • 16,339
  • 1
  • 39
  • 60
0

This is the default behavior where tomcat automatically re-deploys the application when /WEB-INF/web.xml file is updated.

As per the documentation at http://tomcat.apache.org/tomcat-9.0-doc/deployer-howto.html#Deploying_on_a_running_Tomcat_server :

"If the Host autoDeploy attribute is "true", the Host will attempt to deploy and update web applications dynamically, as needed, for example if a new .WAR is dropped into the appBase. For this to work, the Host needs to have background processing enabled which is the default configuration.

autoDeploy set to "true" and a running Tomcat allows for:

Re-loading of a web application if the /WEB-INF/web.xml file (or any other resource defined as a WatchedResource) is updated."

To bypass the default behavior set autoDeploy to false and restart the tomcat. You can update the server.xml to set autoDeploy to false value.The file located at $CATALINA_BASE/conf. Tag name is Host as given below

    <Host name="localhost"  appBase="webapps"
        unpackWARs="true" autoDeploy="true">

I can only update the "server.xml" file, although the documentation says : In earlier versions of Tomcat the content of a Context Descriptor configuration was often stored within Tomcat's primary configuration file server.xml but this is now discouraged (although it currently still works).

So in answer to your query , no there has been no change with respect to setting autoDeploy to true or false as per the documentations of tomcat 5.x.x and tomcat 9.x.x. I am also running tomcat 9.x.x as I verify this default behavior by setting autoDeploy to false .