File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Services Certification (SCDJWS/OCEJWSD) and the fly likes JAX-RS resource lifecycle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Services Certification (SCDJWS/OCEJWSD)
Bookmark "JAX-RS resource lifecycle" Watch "JAX-RS resource lifecycle" New topic

JAX-RS resource lifecycle

Souvik Dasgupta
Ranch Hand

Joined: Sep 24, 2009
Posts: 95

Consider the below code:


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.

Thanks and Regards
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1341

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.
Souvik Dasgupta
Ranch Hand

Joined: Sep 24, 2009
Posts: 95

I agree. Here's the link:
subject: JAX-RS resource lifecycle
It's not a secret anymore!