这是一个简单的例子。
在您的 jetty-distribution 目录中,您有一个目录(通过文件中的配置/resources/
默认包含在服务器级别的类加载器中)OPTIONS
/start.ini
如果您创建/resources/myconfig.properties
(例如)具有以下内容:
food=fruit
fruit.color=yellow
fruit.name=banana
然后你可以让一个 Servlet 在 init() 上加载它,如下所示:
public class LoadResourceServlet extends HttpServlet
{
private Properties props;
@Override
public void init(ServletConfig config) throws ServletException
{
super.init(config);
props = new Properties();
URL url = this.getClass().getResource("/myconfig.properties");
if (url != null)
{
try (InputStream stream = url.openStream())
{
props.load(stream);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
resp.setContentType("text/plain");
try (PrintWriter writer = resp.getWriter())
{
writer.printf("food = %s%n",props.getProperty("food"));
writer.printf("fruit.color = %s%n",props.getProperty("fruit.color"));
writer.printf("fruit.name = %s%n",props.getProperty("fruit.name"));
}
}
}