I am creating a Vaadin web application (Deployable war file) to be hosted by tomcat.
Is there a way to do something (let's say creating an object) at the deployment of war file, before initialization or before getting a request from a client?
Could it be done by overriding
void init()
method? I don't have a clear idea. I'm new to this.
When the server is started or more precisely when the servlet container starts up, it deploys all the web applications, loads them and then it creates an application context for each application and store in its memory. I mentioned the above things so that you can have a better understanding of the solution to your question.
Now coming to your question, what you can do is create a class and name it anything and then implement ServletContextListener interface. It has basically two methods with the following signature.
Now in the contextInitialized method, you can do whatever you want, like creating an object or something because this is the method which is invoked when your ServletContext is initialized.
In your web.xml place the mapping as follows
<listener>
<listener-class>
your fully qualified class name that which will implement the ServletContextListener
</listener-class>
</listener>
I hope it answers your question. Happy to help.