I'm kind of new in web development with Java. I am developing a web service and I've chosen REST / Jersey for it.
I want to init some stuff on startup of the service and to keep them all along the life of the service.
First question : Is the constructor of the Jersey Servlet a good place to do that ?
Basically, what I want to do is to load a config.ini file located in my WEB-INF directory. Following this help, I understand I need a ServletContext to load my file as a resource.
However, it is not clear to me how to get this ServletContext in a Jersey Servlet, as it is not really an instance of a servlet, but rather a POJO with some annotations. I wanted to try this tip, but the attribute "context" is null in the constructor. I think that Jersey might populate it after the constructor. Right ?
So how is the right way to do this ?
Here is my code so far :
/** Main REST servlet */
@Path("/")
public class Servlet {
// ----------------------------------------------------
// Constants
// ----------------------------------------------------
static private final String CONFIG_PATH = "/WEB-INF/config.ini";
// ----------------------------------------------------
// Attributes
// ----------------------------------------------------
/** Context */
@Context ServletContext context;
// ----------------------------------------------------
// Constructor
// ----------------------------------------------------
/** Init the servlet */
public Servlet() {
// Load config.ini from WEB-INF
Config.config = new Config(
this.context.getResourceAsStream(CONFIG_PATH));
// FAIL! this.context is null ...
}
// ----------------------------------------------------
// URI Handlers
// ----------------------------------------------------
/** Welcome page */
@GET
@Path("/")
@Produces(MediaType.TEXT_HTML)
public String welcome() {
return "<h1>Hi there.</h1>";
}
}
Any help would be much appreciated. Thanks in advance,
Raphael
Using a ContextListener isn't the JAX-RS way to do this-- the ContextListener is the "next layer down," as it were, in the servlet API.
You can put the annotated declaration in the constructor parameters and it will be passed to the constructor:
public Servlet(@Context ServletContext context) {
Using the @Context annotation for a data member initializes that member after the object is constructed, as you discovered.
Another way to do this is to move the initialization of the Config object into some sort of init call, and only initialize it when it's needed, i.e. lazy initialization. Then the ServletContext data member will already have been set by the Jersey framework.
Good Luck!
Kirk
I am not familiar with Jersey, but generally in a Java web application, I think the right thing to do would be to create a ContextListener.
A context listener is a class that implements the interface javax.servlet.ServletContextListener and is configured in your web.xml. It has a method that is executed when the application is first loded into your container, and another one that is executed when the application is stopped, so it is the ideal place to put some one-time initialization stuff, and clean-up things before the application is stopped.
So the steps are :
You'll find additional info here : tutorial
Or on Sun's, er, Oracle's site.
By the way, if your file will be in a JAR file I am not sure that the ServletContext method is the best way to load it. I think you're better off with somethinbg like :
this.getClass().getClassLoader().getResourceAsStream("com.company.my.file.properties");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With