Servlet init() method equivalent in JAX-RS

denizdurmus picture denizdurmus · May 29, 2013 · Viewed 24.4k times · Source

I am working on an application which is running on Glassfish. I am supposed to convert the servlets to proper restful stuff, by using jax-rs and jersey.

I have been trying to find a workaround for init() method, but till now i failed.

Here is the original part, using servlets:

import javax.servlet.*

public void init(ServletConfig config) throws ServletException {
super.init(config);
 if (!isRunning() == true)) {
     /* Do some stuff here*/
 }

 logger.info("Deamon has started");
}

and this one which i am trying to use jax-rs

import javax.ws.rs.*
import javax.servlet.*

public void init(@Context ServletConfig config) throws ServletException {
//uper.init(config);
if (!isRunning() == true)) {
  /* Do some stuff here*/
}

logger.info("Deamon has started");
}

I have checked mailing lists and googled around but couldnt find a way which could work for this case.

any ideas how to achieve the same behaviour with servlets for init method?

Answer

denizdurmus picture denizdurmus · May 31, 2013

finally, after googling a little bit more, i found a proper solution.

basically, i have extended public class ContextListener implements ServletContextListener class and implemented the abstract method public void contextInitialized(ServletContextEvent sce) which is called when the application is loaded. I have moved the logic from the servlet to here for doing the initialization and other config settings, then it was smooth.