[英]How can I use Jetty to serve servlets without worrying about War file layouts?
I have a pre-existing Java application and I would like to expose a web UI using Vaadin. 我有一个预先存在的Java应用程序,我想使用Vaadin公开一个Web UI。 I'm using Maven for dependency management.
我正在使用Maven进行依赖管理。
The Vaadin documentation suggest using a war file layout, but I don't want to have to rearrange my codebase into the standard War format. Vaadin文档建议使用war文件布局,但我不想将我的代码库重新排列为标准War格式。
Is there a way that I can programmatically start a Jetty server and get it to serve up a servlet, without having to worry about war directory structures? 有没有一种方法可以以编程方式启动Jetty服务器并使其服务于servlet,而不必担心war目录结构?
Some example code showing how to serve up a servlet from a main() method would be very helpful here. 一些示例代码展示了如何通过main()方法提供服务于servlet,这将非常有帮助。
Alternatively, if something other than Jetty would work better here, that would be good to know. 或者,如果Jetty以外的其他工具在这里可以更好地工作,那将是个好消息。
It is pretty straightforward to set up a simple HTTP server in-process with jetty: 使用jetty设置简单的HTTP服务器进程非常简单:
final Server httpServer = new Server(18080);
httpServer.setHandler(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.getWriter().write("This is the HTTP response");
}
});
httpServer.start();
Note that this is based on jetty 8.1.8. 请注意,这是基于码头8.1.8的。 The code above does not use Servlets, but it is pretty easy to wire it to any framework you want.
上面的代码不使用Servlet,但是将其连接到所需的任何框架都非常容易。
If you really need a servlet (maybe you already have it ready) use Jetty's ServletContextHandler class instead of your own handler. 如果确实需要servlet(也许已经准备好了),请使用Jetty的ServletContextHandler类而不是您自己的处理程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.