java - 在jetty中部署静态内容

标签 java jetty jetty-9

我正在尝试在 jetty 中静态部署应用程序,但使用 xml 配置文件,因为我使用的是虚拟主机。

我创建此文件 xml 进行部署:

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" 
  "http://www.eclipse.org/jetty/configure.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
    <Set name="contextPath">/mail</Set>
    <!--<Call name="setInitParameter">
        <Arg>org.eclipse.jetty.servlet.Default.useFileMappedBuffer</Arg>
        <Arg>false</Arg>
    </Call> -->
    <Set name="handler">
    <New class="org.eclipse.jetty.server.handler.ResourceHandler">
      <Set name="resourceBase">/ccmail</Set>
      <Set name="directoriesListed">true</Set>
    </New>
  </Set>

    <Set name="virtualHosts">
    <Array type="java.lang.String">
      <Item>apps.cairunet.ad.br</Item>     
    </Array>
  </Set> 

</Configure>

我的文件夹应用程序的名称是 ccmail 。位于webapps/ccmail 我已经尝试传递到路径:

<Set name="resourceBase">/ccmail</Set>

<Set name="resourceBase">ccmail</Set>

<Set name="resourceBase">./ccmail</Set>

<Set name="resourceBase">webapps/ccmail</Set>

<Set name="resourceBase">/webapps/ccmail</Set>

这条路径不适合我的任何人。

Jetty 启动此错误:

2019-02-25 09:36:46.422:WARN:oejs.ServletContextHandler:main: ServletContextHandler.setHandler should not be called dire ctly. Use insertHandler or setSessionHandler etc. 2019-02-25 09:36:46.484:WARN:oejw.WebInfConfiguration:main: Can't generate resourceBase as part of webapp tmp dir name: java.lang.IllegalStateException: No resourceBase or war set for context 2019-02-25 09:36:46.500:WARN:oejw.WebAppContext:main: Failed startup of context o.e.j.w.WebAppContext@7d0587f1{/mail,nul

l,UNAVAILABLE,apps.cairunet.ad.br} java.lang.IllegalStateException: No resourceBase or war set for context at org.eclipse.jetty.webapp.WebInfConfiguration.unpack(WebInfConfiguration.java:577) at org.eclipse.jetty.webapp.WebInfConfiguration.preConfigure(WebInfConfiguration.java:152) at org.eclipse.jetty.webapp.WebAppContext.preConfigure(WebAppContext.java:506) at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:544) at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) at org.eclipse.jetty.deploy.bindings.StandardStarter.processBinding(StandardStarter.java:46) at org.eclipse.jetty.deploy.AppLifeCycle.runBindings(AppLifeCycle.java:192) at org.eclipse.jetty.deploy.DeploymentManager.requestAppGoal(DeploymentManager.java:505) at org.eclipse.jetty.deploy.DeploymentManager.addApp(DeploymentManager.java:151) at org.eclipse.jetty.deploy.providers.ScanningAppProvider.fileAdded(ScanningAppProvider.java:180) at org.eclipse.jetty.deploy.providers.WebAppProvider.fileAdded(WebAppProvider.java:453) at org.eclipse.jetty.deploy.providers.ScanningAppProvider$1.fileAdded(ScanningAppProvider.java:64) at org.eclipse.jetty.util.Scanner.reportAddition(Scanner.java:610) at org.eclipse.jetty.util.Scanner.reportDifferences(Scanner.java:529) at org.eclipse.jetty.util.Scanner.scan(Scanner.java:392) at org.eclipse.jetty.util.Scanner.doStart(Scanner.java:313) at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) at org.eclipse.jetty.deploy.providers.ScanningAppProvider.doStart(ScanningAppProvider.java:150) at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) at org.eclipse.jetty.deploy.DeploymentManager.startAppProvider(DeploymentManager.java:579) at org.eclipse.jetty.deploy.DeploymentManager.doStart(DeploymentManager.java:240) at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:138) at org.eclipse.jetty.server.Server.start(Server.java:415) at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:117) at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:113) at org.eclipse.jetty.server.Server.doStart(Server.java:382) at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) at org.eclipse.jetty.xml.XmlConfiguration$1.run(XmlConfiguration.java:1572) at org.eclipse.jetty.xml.XmlConfiguration$1.run(XmlConfiguration.java:1512) at java.security.AccessController.doPrivileged(Native Method) at org.eclipse.jetty.xml.XmlConfiguration.main(XmlConfiguration.java:1511) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.eclipse.jetty.start.Main.invokeMain(Main.java:220) at org.eclipse.jetty.start.Main.start(Main.java:490) at org.eclipse.jetty.start.Main.main(Main.java:77) 2019-02-25 09:36:46.578:INFO:oejs.AbstractConnector:main: Started ServerConnector@27808f31{HTTP/1.1,[http/1.1]}{0.0.0.0: 8080} 2019-02-25 09:36:46.594:INFO:oejs.Server:main: Started @2970ms

最佳答案

Note: be careful with your DOCTYPE, what you have declared is from Jetty 7.x thru Jetty 8.x, and is not correct for Jetty 9.x

不要混合使用 ResourceHandler 和 WebAppContext/ServletContextHandler。

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN"
   "http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <Set name="contextPath">/mail</Set>
  <Set name="virtualHosts">
    <Array type="java.lang.String">
      <Item>apps.cairunet.ad.br</Item>     
    </Array>
  </Set> 
</Configure>

最基本的支持就是不引用/ccmail在你的<Configure> .

事实上它以 ${jetty.base}/webapps/ccmail/ 的形式存在就足够了,这将部署 /ccmail作为您的静态资源库。

但是如果您想将静态资源与虚拟主机结合起来,那么您可以使用具有备用基础的 WebAppContext 或新的 ResourceHandler。

替代碱基示例: Serving static files from alternate path in embedded Jetty

ResourceHandler 使用示例: https://www.eclipse.org/jetty/documentation/current/static-content-deployment.html

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN"
   "http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure class="org.eclipse.jetty.server.handler.ContextHandler">
  <Set name="contextPath">/ccmail</Set>
  <Set name="handler">
    <New class="org.eclipse.jetty.server.handler.ResourceHandler">
      <Set name="resourceBase">/fully/qualified/path/to/my/jetty.base/webapps/ccmail</Set>
      <Set name="directoriesListed">true</Set>
    </New>
  </Set>
  <Set name="virtualHosts">
    <Array type="java.lang.String">
      <Item>apps.cairunet.ad.br</Item>     
    </Array>
  </Set> 
</Configure>

关于java - 在jetty中部署静态内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54867792/

相关文章:

java - Jetty 在多个 TCP 端口上发布端点

java - jetty 9 : Setting up handlers and connectors

java - Spring 安全 : forcing https using annotations?

java - 错误 : Alias name [null] does not identify a key entry during implementing SSL in Tomcat

gradle - IllegalArgumentException:对象的类型不是org.eclipse.jetty.plus.jndi.Resource类

jetty - 如何将 CXFNonSpringJaxrsServlet 与 serviceBean 而不是 serviceClass 一起使用?

java - 在java中读取csv文件的库

java - Spring mvc @RequestBody如何用@EmbeddedId解析JPA实体

java - 如何在单元测试中站起来然后关闭 Solr 实例?

java - 我可以在同一实例中使用 WebSocketHandler 运行 WebAppContext 吗?