[英]How do I set up static resources and custom services with embedded Jetty?
I'm trying to set up a simple webservice for my application by embedding Jetty. 我正在尝试通过嵌入Jetty为我的应用程序设置一个简单的Web服务。 I'd like to have two different web services available, a simple HTTP server that just serves up static content (which will eventually be a GWT app) and a custom servlet which can spit out JSON status messages for the application. 我想提供两种不同的Web服务,一个简单的HTTP服务器,它仅提供静态内容(最终将成为GWT应用程序),以及一个自定义的Servlet,可以为应用程序吐出JSON状态消息。
My distribution folder structure looks something like this: 我的分发文件夹结构如下所示:
+ dist/
- MyApp.jar
+ lib/
+ html/
- index.html
And here's what I have so far for configuring the embedded server. 到目前为止,这是我配置嵌入式服务器的内容。 I correctly get my test output from my custom servlet when visiting http://localhost/data/
, but I can't seem to get the DefaultServlet to find my index.html file. 访问http://localhost/data/
,我可以从自定义servlet正确获取测试输出,但似乎无法通过DefaultServlet来找到index.html文件。
public Webserver(int port) {
server = new Server(port);
ServletContextHandler context = new ServletContextHandler();
context.setResourceBase("./html/");
server.setHandler(context);
JsonDataApiServlet dataServlet = new JsonDataApiServlet();
DefaultServlet staticServlet = new DefaultServlet();
context.addServlet(new ServletHolder(dataServlet), "/data/*");
context.addServlet(new ServletHolder(staticServlet), "/*");
}
It seems like this would be a common task for people embedding Jetty in things.. am I even on the right track? 对于那些将Jetty嵌入事物中的人来说,这似乎是一项常见的任务。我是否走在正确的轨道上?
Edit 编辑
Turns out this problem was due to a misunderstanding of the way relative paths are calculated inside Jetty. 事实证明,这个问题是由于对Jetty内部相对路径的计算方式存在误解。 I was running this from one folder above the dist folder, using java -jar dist\\MyApp.jar
, and Jetty was looking for dist\\..\\html
rather than the correct dist\\html
. 我使用java -jar dist\\MyApp.jar
从dist文件夹上方的一个文件夹中运行此文件,而Jetty则在查找dist\\..\\html
而不是正确的dist\\html
。 Running the jar from inside the dist folder fixes the issue. 从dist文件夹中运行jar即可解决此问题。 I'll answer with how I made it work without having to run from inside the dist directory. 我将回答如何使其工作而不必从dist目录中运行。
As the edit says, this was just an issue with the directory I was running the jar from. 正如编辑所说,这只是我从中运行jar的目录的问题。 Here is the method I used to find the html folder from wherever the Jar was run from: 这是我用来从运行Jar的位置查找html文件夹的方法:
First, I added the html folder to the Jar's Manifest Class-Path. 首先,我将html文件夹添加到Jar的清单类路径中。 The following code gives the html folder for wherever the Jar is loaded from: 以下代码提供了从何处加载Jar的html文件夹:
ClassLoader loader = this.getClass().getClassLoader();
File indexLoc = new File(loader.getResource("index.html").getFile());
String htmlLoc = indexLoc.getParentFile().getAbsolutePath();
This uses the Classloader to find the index file in the classpath, then finds the absolute directory to pass to Jetty: 这使用Classloader在类路径中找到索引文件,然后找到要传递给Jetty的绝对目录:
server = new Server(port);
ServletContextHandler context = new ServletContextHandler();
context.setResourceBase(htmlLoc);
context.setWelcomeFiles(new String[] { "index.html" });
server.setHandler(context);
JsonDataApiServlet dataServlet = new JsonDataApiServlet();
DefaultServlet staticServlet = new DefaultServlet();
context.addServlet(new ServletHolder(dataServlet), "/data/*");
context.addServlet(new ServletHolder(staticServlet), "/*");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.