views:

39

answers:

1

Hello,

I'm kind of new in web development with Java. I am developing a web service and I've chosen REST / Jersey for it.

I want to init some stuff on startup of the service and to keep them all along the life of the service.

First question : Is the constructor of the Jersey Servlet a good place to do that ?

Basically, what I want to do is to load a config.ini file located in my WEB-INF directory. Following this help, I understand I need a ServletContext to load my file as a resource.

However, it is not clear to me how to get this ServletContext in a Jersey Servlet, as it is not really an instance of a servlet, but rather a POJO with some annotations. I wanted to try this tip, but the attribute "context" is null in the constructor. I think that Jersey might populate it after the constructor. Right ?

So how is the right way to do this ?

Here is my code so far :

/** Main REST servlet */
@Path("/")
public class Servlet {

    // ---------------------------------------------------- 
    // Constants                     
    // ---------------------------------------------------- 

    static private final String CONFIG_PATH = "/WEB-INF/config.ini";

    // ---------------------------------------------------- 
    // Attributes                     
    // ---------------------------------------------------- 

    /** Context */
    @Context ServletContext context;

    // ---------------------------------------------------- 
    // Constructor                     
    // ---------------------------------------------------- 

    /** Init the servlet */
    public Servlet() {

        // Load config.ini from WEB-INF
        Config.config = new Config(
                this.context.getResourceAsStream(CONFIG_PATH));

        // FAIL! this.context is null ...

    }

    // ---------------------------------------------------- 
    // URI Handlers                    
    // ---------------------------------------------------- 

    /** Welcome page */
    @GET
    @Path("/")
    @Produces(MediaType.TEXT_HTML)
    public String welcome() {
        return "<h1>Hi there.</h1>";
    }
}

Any help would be much appreciated. Thanks in advance,

Raphael

+2  A: 

I am not familiar with Jersey, but generally in a Java web application, I think the right thing to do would be to create a ContextListener.

A context listener is a class that implements the interface javax.servlet.ServletContextListener and is configured in your web.xml. It has a method that is executed when the application is first loded into your container, and another one that is executed when the application is stopped, so it is the ideal place to put some one-time initialization stuff, and clean-up things before the application is stopped.

So the steps are :

  1. create your Listener class, implement the contextInitialized(ServletContextEvent sce) method. In this method you receive a ServeltContextEvent that has getServletContext() method that gives you access to the ServletContext.
  2. Configure your Listener in your web.xml

You'll find additional info here : tutorial

Or on Sun's, er, Oracle's site.

By the way, if your file will be in a JAR file I am not sure that the ServletContext method is the best way to load it. I think you're better off with somethinbg like :

this.getClass().getClassLoader().getResourceAsStream("com.company.my.file.properties");
Pierre Henry
Hi, thanks for your help.This seems more clear this way, and this works !
Raphael Jolivet