users@jersey.java.net

[Jersey] Re: Jersey2: How to pass parameters from web.xml to the Application?

From: Van Klaveren, Brian N. <bvan_at_slac.stanford.edu>
Date: Fri, 25 Oct 2013 15:27:40 -0700

Hi,

Sorry, I double checked because I thought context-param added it to the environment as well, but it only adds it to a servlet context. I think the first part is right, however, if you want to do it not from a servlet, you need to define it in web.xml the following way:

    <env-entry>
        <env-entry-name>maybeDebug</env-entry-name>
        <env-entry-value>nope</env-entry-value>
        <env-entry-type>java.lang.String</env-entry-type>
    </env-entry>

The second bit of code remains the same. You should be able to retrieve, anywhere in the application, with the following code:

String paramValue = (String) new InitialContext().lookup("java:comp/env/maybeDebug");

Brian


On Oct 25, 2013, at 2:50 PM, cowwoc <cowwoc_at_bbs.darktech.org> wrote:

> Brian,
>
> I'm trying to look up the parameter value from inside a class that
> extends ResourceConfig. I don't have access to ServletConfig from there,
> do I? This is in spite of the fact that Jersey is running on top of
> ServletContainer.
>
> Gili
>
> On 25/10/2013 5:44 PM, Van Klaveren, Brian N. wrote:
>> In web.xml:
>>
>> <context-param>
>> <param-name>maybeDebug</param-name>
>> <param-value>nope</param-value>
>> </context-param>
>>
>>
>> In servlet:
>> String maybeDebug = getServletConfig().getInitParameter(“maybeDebug”);
>>
>> In non-servlet:
>> InitialContext context = new InitialContext();
>> String paramValue = (String) context.lookup("java:comp/env/maybeDebug");
>>
>>
>> Brian
>>
>> On Oct 25, 2013, at 2:24 PM, cowwoc <cowwoc_at_bbs.darktech.org> wrote:
>>
>>> Jeez. That's very ugly.
>>>
>>> Isn't there a way for me to pass variables from web.xml to
>>> ServletContainer and from there to ResourceConfig.getProperties()? I
>>> haven't tried this yet because the migration process is not complete,
>>> but surely there should be a way to do this using existing Jersey
>>> mechanisms...?
>>>
>>> Gili
>>>
>>> On 25/10/2013 4:42 PM, Joseph Mocker wrote:
>>>> A slightly more elegant way than Ivar’s suggestion would be to put a properties file in the classpath somewhere (for example, with Tomcat, you could put it in tomcat’s lib/) folder, then in your ResourceConfig, do a getResourceAsStream(“/config.properties”) and look for some property to tell you the mode you are running in.
>>>>
>>>>
>>>> Joseph Mocker | Velti | Senior Software Architect
>>>> t +1.415.315.4314 m +1.650.566.7033
>>>> e jmocker_at_velti.com @VeltiMobile
>>>>
>>>> The leading global technology provider of
>>>> mobile marketing and advertising solutions
>>>>
>>>> On Oct 25, 2013, at 12:46 PM, Ivar <ivarconr_at_gmail.com> wrote:
>>>>
>>>>> Use regular jvm properties to set a debug property?
>>>>>
>>>>> ​Ivar
>