简体   繁体   中英

Start java application with jetty without WAR file

I try start Jersey + Jetty + Nginx via this tutorial and I cannot use war file. How can I start my java application?

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 .

Project structure is described here.

/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? I know that I have redirect requests from nginx to jetty, but I don't understand how can I start jersey application with jetty...

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.

Your prior question sets up a com.sun.net.httpserver.HttpServer which is not the same thing.

resourceBase is the root directory for any webapp content that you might want to serve.

Since you are using a simple ContextHandler , then that should point to a directory on your disk.

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)

The ResourceHandler you have defined should use the ContextHandler.resourceBase .

Be aware that ResourceHandler is for the barest, most simplistic static file serving. 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 .

Also, if all you want is a file server in embedded jetty, why are you using an XML deployment with a basic ContextHandler ? 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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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