views:

919

answers:

4

I'm playing around with jax-rs , deploying on tomcat (handling via com.sun.ws.rest.spi.container.servlet.ServletContainer). It's basically

@Path("/hello")
@Produces({"text/plain"})
public class Hellohandler{
    @GET
    public String hello() {
      return "Hello World";
    }
}

Is there any way I can get hold of the ServletContext within my HelloHandler ?

A: 

Just use resource injection like this,

@Resource ServletContext servletContext;
ZZ Coder
A: 

Furthermore, @Resource annotation might not work. Try this

@javax.ws.rs.core.Context 
ServletContext context;
Adeel Ansari
which javax.ws.rs class do I use as a servlet handler ?
leeeroy
Not really getting you by this. You are already having a class which handles HTTP GET requests. Please elaborate more what do mean by Servlet Handler. Cheers.
Adeel Ansari
A servlet container needs a servlet to handle requests. com.sun.ws.rest.spi.container.servlet.ServletContainer takes care of handling the requests and dispatch them to my annotaded classes. You imply I should not use com.sun.* classes, so what then should I use ?
leeeroy
Why not use any popular servlet container like Jetty or Tomcat?
Adeel Ansari
As the question says I am using Tomcat. Tomcat will not blindly recognize my jax-rs annotated classes. It will need a servlet to handle the requests. Same deal with Jetty.
leeeroy
com.sun.ws.rest.spi.container.servlet.ServletContainer is what you use if you're using the jax-rs reference implementations. Other implementation will require you to use their implementation specific servlet. Alternativly, you can(according to the spec) derive from javax.ws.rs.core.Application and specify that as your servlet handler, though that is not supported by implementations yet..
nos
I agree with 'nos' here. I merely said that because they discourage to use classes under `com.sun...`
Adeel Ansari
How about other implementations? Namely Jersey. The package would be `com.sun.jersey.spi.container.servlet.ServletContainer` in that case. Although, this also starts like `com.sun...`, but I think this must be excluded from those they discouraged. I don't know why they come up with this package scheme, when they themselves discourge the thing. Its confusing.
Adeel Ansari
A: 

Wow this is a simple question. Can someone provide a simple answer that works?

snoop