9

I'm creating a webapp in Tomcat using jersey. I haven't created a Servlet, I just use the jersey ServletContainer and some Resource classes.

My web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">
    <servlet>
        <servlet-name>Jersey REST Service</servlet-name>
        <servlet-class>
            com.sun.jersey.spi.container.servlet.ServletContainer
        </servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.mycompany.myproduct.rest</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey REST Service</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

My webapp needs to read some configuration values. I have the impression that a good way to do this is with context-Params, like this:

<web-app>
   ...
  <context-param>
    <description>This is a context parameter example</description>
    <param-name>ContextParam</param-name>
    <param-value>ContextParam value</param-value>
  </context-param>
</web-app>

Is this the best way? How can I access these context params from my resource classes?

Here's an example resource class:

@Path("/api/ping")
public class PingResource {
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String helloWorld() {
        return "pong";
    }
}
tallseth
  • 3,635
  • 1
  • 23
  • 24

2 Answers2

12

You can inject the ServletContext and look up the parameters from there. Something like:

public class PingResource {

    @Context ServletContext context;

    public String myServiceMethod() {
       context.getInitParam("ContextParam");
    }

}
Gili
  • 86,244
  • 97
  • 390
  • 689
Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
  • This looks good. But I assume it returns an init param. What about a context param? I don't see how to get it from a ServletContext. – tallseth Jun 26 '13 at 18:33
  • Not quite...check out http://stackoverflow.com/questions/2069902/dd-elements-context-param-and-init-param-both-use-the-getinitparameter-me – Jeff Storey Jun 26 '13 at 18:38
  • 1
    oh, I'm actually not sure if `@Resource` injection is fully implemented yet in Jersey if you're not using Spring/Guice. You should be able to use `@Context` instead. Example updated. – Jeff Storey Jun 26 '13 at 21:46
  • for me it will ask to create @Context annotatron – R.Anandan Mar 15 '17 at 07:58
4

Below is snapshot that worked for me :)

// add imports

import javax.servlet.ServletContext;
import javax.ws.rs.core.Context;

//add property in your class

@Context
ServletContext context;

// Use the context param in your methods

String companyName = this.context.getInitParameter("companyName");

// add context to web.xml

<context-param>
    <description>Context Parameter Test</description>
    <param-name>companyName</param-name>
    <param-value>Test Organization, Incorporated</param-value>
</context-param>
Arun Kumar
  • 113
  • 1
  • 6