简体   繁体   English

如何使用Jetty服务servlet,而不必担心War文件的布局?

[英]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.

相关问题 在java中如何连接到https站点而不必担心安全证书 - In java how can I connect to https sites without worrying about security certificates 如何在Jetty中将多个上下文映射到同一战争文件? - How can I map multiple contexts to the same war file in Jetty? 如何在嵌入式Jetty 8中部署WAR? - How can I deploy a WAR in an embedded Jetty 8? 如何在jetty服务器上使用war文件之外的文件? - How to use files outside of war file on jetty server? 如何使用Jetty为Angular应用程序提供服务,还可以使用与Jersey相同的服务器? - How can I serve an Angular app using Jetty and also use the same server along with Jersey? 如何将 a.war 文件编译到 i-jetty(android web 服务器)? - How to compile a .war file to i-jetty (android web server)? 在没有maven的情况下启动jetty的可执行war文件 - Executable war file that starts jetty without maven 在没有WAR文件的情况下使用Jetty启动Java应用程序 - Start java application with jetty without WAR file 码头无法启动战争档案 - Jetty can't start war file Jetty不会将我的WAR文件提取到temp目录中。 如何阻止其提取或确保将其提取到temp目录? - Jetty is not extracting my WAR file into temp directory. How can I stop it from extracting or make sure it extracts to the temp directory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM