According to JAX-RS spec: "By default a new resource class instance is created for each request to that resource" and also "Root resource classes are instantiated by the JAX-RS runtime and MUST have a public constructor for which the JAX-RS runtime can provide all parameter values.".
I found is that constructor fired before I actually hit the JAX-RS resource.Message inside constructor got printed when I hit index.jsp. My question is - Is this the way it works? I thought It will get fired when JAX-RS resource is actually hit.
I will try to answer your query
JAX-RS resource classes are simple POJOs. Resource has been defined with Servlet settings in web.xml with <load-on-startup> parameter as 1. This load-on-startup element is causing Jersey Servlet to load JAX-RS resource before first request.
If you do not specify a <load-on-startup> element, the servlet container will typically load your servlet when the first request arrives for it.
By setting a <load-on-startup> element, you can tell the servlet container to load the servlet as soon as the servlet container starts.
So try removing this load-on-startup element and then try to run your example.