Apache CXF - getting ServletContext in code

less than 1 minute read

It is common to get a Servlet context on implementing WEB API. Apache CXF has a little different approach to get this instance. The following code show how to get ServletContext instance for RS.

1
2
3
4
5
6
7
8
9
10
public interface IServiceInterface {
    void SampleFunction();
}
public class ImplementClass implement IServiceInterface {
    @Context
    private org.apache.cxf.jaxrs.ext.MessageContext messgeContext;
    public void SampleFunction() {
        ServletContext servletContext = messgeContext.getServletContext();
    }
}