Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Servlet init() method equivalent in JAX-RS

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?

like image 913
denizdurmus Avatar asked May 29 '13 08:05

denizdurmus


1 Answers

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.

like image 167
denizdurmus Avatar answered Oct 11 '22 17:10

denizdurmus