简体   繁体   English

在没有WAR文件的情况下使用Jetty启动Java应用程序

[英]Start java application with jetty without WAR file

I try start Jersey + Jetty + Nginx via this tutorial and I cannot use war file. 我尝试通过本教程启动Jersey + Jetty + Nginx, 但是我无法使用war文件。 How can I start my java application? 如何启动我的Java应用程序?

I start application by right click on BackendServer.java and click "Run" in IDEA or using in terminal java -cp /home/example/backend/build/WEB-INF/lib/backend.jar:/home/example/backend/libs/* com.example.backend.BackendServer . 我通过右键单击BackendServer.java来启动应用程序,然后在IDEA中单击“运行”或在终端java -cp /home/example/backend/build/WEB-INF/lib/backend.jar:/home/example/backend/libs/* com.example.backend.BackendServer

Project structure is described here. 项目结构在这里描述

/opt/jetty/webapps/backend.xml /opt/jetty/webapps/backend.xml

<?xml version="1.0"  encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC
    "-//Mort Bay Consulting//DTD Configure//EN"
    "http://www.eclipse.org/jetty/configure_9_0.dtd">

<!--
  Configure a custom context for serving javadoc as static resources
-->

<Configure class="org.eclipse.jetty.server.handler.ContextHandler">
  <Set name="contextPath">/</Set>
  <Set name="resourceBase"><SystemProperty name="jetty.home" default="."/>??????????</Set>
  <Set name="handler">
    <New class="org.eclipse.jetty.server.handler.ResourceHandler">
      <Set name="welcomeFiles">
        <Array type="String">
          <Item>index.html</Item>
        </Array>
      </Set>
      <Set name="cacheControl">max-age=3600,public</Set>
    </New>
  </Set>
</Configure>

What should be instead ??????? 应该是什么,而不是??????? ? Should I use embedded jetty in BackendServer.java? 我应该在BackendServer.java中使用嵌入式码头吗? I know that I have redirect requests from nginx to jetty, but I don't understand how can I start jersey application with jetty... 我知道我已经将请求从Nginx重定向到了码头,但我不知道如何使用码头来启动球衣应用程序...

The linked example/tutorial and the linked earlier question are not compatible. 链接的示例/教程和链接的先前的问题不兼容。

The tutorial is for Jetty 6 (now impossibly out of date), and uses embedded-jetty completely, with deployments and everything enabled. 本教程适用于Jetty 6(现在已经过时了),并且完全使用了嵌入式jetty,并启用了部署和所有功能。

Your prior question sets up a com.sun.net.httpserver.HttpServer which is not the same thing. 您之前的问题设置了一个com.sun.net.httpserver.HttpServer ,它不是同一回事。

resourceBase is the root directory for any webapp content that you might want to serve. resourceBase是您可能要提供的任何Webapp内容的根目录。

Since you are using a simple ContextHandler , then that should point to a directory on your disk. 由于您使用的是简单的ContextHandler ,因此应该指向磁盘上的目录。

If you were using a WebAppContext , then that should point to your webapp base directory (where optional files like WEB-INF/web.xml or WEB-INF/classes would be) 如果您使用的是WebAppContext ,则该目录应指向您的webapp基本目录(将包含WEB-INF/web.xmlWEB-INF/classes等可选文件)

The ResourceHandler you have defined should use the ContextHandler.resourceBase . 您定义的ResourceHandler应该使用ContextHandler.resourceBase

Be aware that ResourceHandler is for the barest, most simplistic static file serving. 请注意, ResourceHandler用于最简单,最简单的静态文件服务。 If you have any requirements from your web clients to perform cached lookups, resume downloads, partial downloads, or mime-type controls, then use a DefaultServlet . 如果您的Web客户端对执行缓存的查找,恢复下载,部分下载或mime类型的控件有任何要求,请使用DefaultServlet

Also, if all you want is a file server in embedded jetty, why are you using an XML deployment with a basic ContextHandler ? 另外,如果您只需要嵌入式Jetty中的文件服务器,为什么还要使用带有基本ContextHandler的XML部署? That would be so much easier to just write into your embedded jetty service. 只需将其写入嵌入式码头服务中,将非常容易。

Some embedded jetty example code that might prove useful to you: 一些可能对您有用的嵌入式码头示例代码:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM