Note that there are some explanatory texts on larger screens.

plurals
  1. POGet ServletContext in Application
    primarykey
    data
    text
    <p>Could you possibly explain how I can get the <code>ServletContext</code> instance in my <code>Application</code>'s sub-class? Is it possible? I have tried to do it like in the following snippet but it does not seem to work - the <code>ctx</code> is not set:</p> <pre><code>import javax.ws.rs.core.Application; import javax.ws.rs.core.Context; //... @ApplicationPath("/") public class MainApplication extends Application { @Context ServletContext ctx; @Override public Set&lt;Class&lt;?&gt;&gt; getClasses() { Set&lt;Class&lt;?&gt;&gt; classes = new HashSet&lt;Class&lt;?&gt;&gt;(); //... return classes; } } </code></pre> <p>web.xml:</p> <pre><code>&lt;web-app ...&gt; &lt;context-param&gt; &lt;param-name&gt;environment&lt;/param-name&gt; &lt;param-value&gt;development&lt;/param-value&gt; &lt;/context-param&gt; &lt;filter&gt; &lt;filter-name&gt;jersey-filter&lt;/filter-name&gt; &lt;filter-class&gt;org.glassfish.jersey.servlet.ServletContainer&lt;/filter-class&gt; &lt;init-param&gt; &lt;param-name&gt;javax.ws.rs.Application&lt;/param-name&gt; &lt;param-value&gt;my.MainApplication&lt;/param-value&gt; &lt;/init-param&gt; &lt;/filter&gt; ... &lt;/web-app&gt; </code></pre> <p>The problem is that I need to get context parameters from it. If there is another way, I would be grateful if somebody gave a hint.</p> <hr> <p>I understand that <code>Context</code> annotation might not be purposed for this. Actually, I do not need <code>ServletContext</code> itself. If only I could get context params from web.xml, I would be absolutely happy.</p> <p>Here is an example of what I really need:</p> <pre><code>import java.util.HashSet; import java.util.Set; import javax.servlet.ServletContext; import javax.ws.rs.core.Application; import javax.ws.rs.core.Context; import org.glassfish.hk2.utilities.binding.AbstractBinder; public class MainApplication extends Application { @Context ServletContext ctx; @Override public Set&lt;Object&gt; getSingletons() { Set&lt;Object&gt; set = new HashSet&lt;Object&gt;(); final String environment = ctx.getInitParameter("environment"); //final String environment = ... get context parameter from web xml set.add(new AbstractBinder() { @Override protected void configure() { bind(new BaseDataAccess(environment)).to(DataAccess.class); } }); //... return set; } } </code></pre> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload