How To Read Context Parameters From a Restlet?
Question
When using Restlets, how can I read configuration parameters passed in through web.xml? With servlets, context-param can be used. How do I read context parameters from within a Restlet?
Solution
From the mailing list:
the init parameters are available in the application's context: getApplication().getContext().getParameters().
In web.xml:
<context-param>
<param-name>my.context.param</param-name>
<param-value>Hello World</param-value>
</context-param>
In a Restlet's represent method, use:
// => "Hello World"
String result =
getApplication().getContext().getParameters().getFirstValue("my.context.param");
OTHER TIPS
ServerServlet
adds all the init parameters from both the servletConfig
and from the servletContext
to the application context.
So depending on your need, you could either examine the source code for ServerServlet
, and read the configuration parameters in the same way, or simply obtain the values from your restlet, or your restlet's application's context.