If you really want to do this in a servlet environment, you could potentially have two servlets within your webapp: one for the Restlet application/component and one for your initialization, using load-on-startup
(which you wouldn't necessarily map to any URL, as far as I'm aware you don't have to). This way, you wouldn't have to subclass org.restlet.ext.servlet.ServerServlet
. I think this is probably easier, since that init servlet would just contain init()
, but this would only work for things that do not depend on the Restlet app/component to be initialized first.
<context-param>
<param-name>org.restlet.clients</param-name>
<param-value>HTTP HTTPS CLAP FILE</param-value>
</context-param>
<servlet>
<servlet-name>ExampleInit</servlet-name>
<servlet-class>example.TestInitServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>Example</servlet-name>
<servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
<init-param>
<param-name>org.restlet.application</param-name>
<param-value>example.TestApplication</param-value>
</init-param>
<init-param>
<param-name>org.restlet.autoWire</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Example</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Alternatively (or perhaps in complement to this), I tend to use JNDI for initializing the database connections and a few other configuration settings. This also allows me to keep the same configuration and loading mechanisms, whether I use a Restlet standalone server or Restlet within a webapp.
For example, for deployment within a servlet container (e.g. Jetty or Tomcat), I use the container's JNDI configuration, but for local tests (with a standalone Restlet app), I use the Jetty JNDI context factory (which you can get as a separate jar from the rest of Jetty).