title: Jersey / ServletContext and resource loading on startup
body: <p>I'm kind of new in web development with Java.
  I am developing a web service and I've chosen REST / <em>Jersey</em> for it.</p>
  
  <p>I want to init some stuff on startup of the service and to keep them 
  all along the life of the service.</p>
  
  <p>First question : Is the constructor of the <em>Jersey</em> Servlet a good place to do that ?</p>
  
  <p>Basically, what I want to do is to load a config.ini file located in my WEB-INF directory.
  Following <a href="https://stackoverflow.com/questions/1108434/howto-load-a-resource-from-web-inf-directory-of-a-web-archive">this help</a>, I understand I need a <strong>ServletContext</strong> to load my file as a resource.</p>
  
  <p>However, it is not clear to me how to get this <strong>ServletContext</strong> in a <em>Jersey</em> Servlet, as it is not really an instance of a servlet, but rather a POJO with some annotations.
  I wanted to try <a href="https://stackoverflow.com/questions/1814517/get-servletcontext-on-tomcat-from-jax-rs-jersey/1814788#1814788">this tip</a>, but the attribute "context" is null in the constructor. I think that <em>Jersey</em> might populate it after the constructor. Right ?</p>
  
  <p>So how is the right way to do this ?</p>
  
  <p>Here is my code so far :</p>
  
  <pre><code>/** 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 "&lt;h1&gt;Hi there.&lt;/h1&gt;";
      }
  }
  </code></pre>
  
  <p>Any help would be much appreciated.
  Thanks in advance,</p>
  
  <p>Raphael</p>