Wences Llobet
Wences Llobet

Reputation: 585

How to deploy static content on root context with jetty runner (standalone jar)

Update:

I've noticed that if I'don't deploy the WAR file i can do it as Joakim Erdfelt says, i'll investigate what changes I've done to the war that causes the failure at root context.


I know how to deploy static content to any context :

  sudo java  -cp jetty-runner-9.2.13.v20150730.jar org.eclipse.jetty.runner.Runner  --port 80 MYAPP.war --path /context1 site/MyStaticWebSite 

//will deploy mysite in
//127.0.0.1/context1/index.html

but I want to deploy it in the root

 127.0.0.1/index.html

And there's no reference in the docs nor in google. https://webtide.com/downloads/ If I do :

sudo java  -cp jetty-runner-9.2.13.v20150730.jar org.eclipse.jetty.runner.Runner  --port 80 MYAPP.war --path / site/MyStaticWebSite

I get :

2015-11-11 21:05:27.498:INFO::main: Logging initialized @121ms
2015-11-11 21:05:27.513:INFO:oejr.Runner:main: Runner
2015-11-11 21:05:27.618:INFO:oejs.Server:main: jetty-9.2.13.v20150730
Nov 11, 2015 9:05:34 PM org.glassfish.jersey.server.ApplicationHandler initialize
INFO: Initiating Jersey application, version Jersey: 2.6 2014-02-18 21:52:53...
2015-11-11 21:05:35.323:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@33833882{/,file:/tmp/jetty-0.0.0.0-80-MYAPP.war-_-any-4618238315169821839.dir/webapp/,AVAILABLE}{file:/home/ubuntu/MYAPP.war}
2015-11-11 21:05:35.744:WARN:oeja.AnnotationConfiguration:main: ServletContainerInitializers: detected. Class hierarchy: empty
2015-11-11 21:05:35.871:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@200a570f{/,file:site/MyStaticWebSite/,AVAILABLE}{/root/MyStaticWebSite/}
2015-11-11 21:05:35.872:WARN:oejsh.RequestLogHandler:main: !RequestLog
2015-11-11 21:05:35.890:INFO:oejs.ServerConnector:main: Started ServerConnector@2504eefd{HTTP/1.1}{0.0.0.0:80}
2015-11-11 21:05:35.891:INFO:oejs.Server:main: Started @8517ms

And root webpage shows the following:

Not showing web content

Upvotes: 1

Views: 1118

Answers (2)

Wences Llobet
Wences Llobet

Reputation: 585

I've finally found how I could do it,

Instead of:

sudo java  -cp jetty-runner-9.2.13.v20150730.jar org.eclipse.jetty.runner.Runner  --port 80 MYAPP.war --path / site/MyStaticWebSite

Do:

sudo java  -cp jetty-runner-9.2.13.v20150730.jar org.eclipse.jetty.runner.Runner  --port 80 --path /MYAPP MYAPP.war --path / site/MyStaticWebSite

MYAPP was deploying itself to /MYAPP context as specified in web.xml but didn't allow to deploy any content in the root context, by spec.

Then the problem was that /MYAPP couldn't be reached, but /MYAPP/MYAPP could, that was because the web.xml was saying to deploy the service into /MYAPP so I had to change it

from:

<servlet-mapping>
   <servlet-name>MY_SERVLET_NAME</servlet-name>
   <url-pattern>/MYAPP/*</url-pattern>
</servlet-mapping>

to:

<servlet-mapping>
   <servlet-name>MY_SERVLET_NAME</servlet-name>
   <url-pattern>/*</url-pattern>
</servlet-mapping>

Now it works.

Upvotes: 0

Joakim Erdfelt
Joakim Erdfelt

Reputation: 49555

First, upgrade!

9.1.0.M0 is an unstable/beta milestone build.

Use something a bit more recent and stable (how about 9.2.13.v20150730 of jetty-runner?)

If you want it deployed on the root context, use --path / (not --path /context1)

Update:

Ah, you have 2 webapps, and Jersey in the mix, didn't realize.

  1. WebApp 1: context:/ path:MYAPP.war
  2. WebApp 2: context:/ path:site/MyStaticWebSite

No you can't have both on the same context path, that's not supported by the servlet spec.

This is because every webapp MUST terminate once it is entered.

Scenario:

  • So since the context path is /, both webapps will match
  • First one entered will service the request
  • If a servlet has a matching url-pattern, that servlet is called
  • If no servlet matches, then the DefaultServlet is used.
  • DefaultServlet will look for a static resource matching the url-pattern/pathInfo
  • If no static resource matches, then DefaultServlet produces an error.

That webapp must produce a response. There is no opportunity to skip the webapp and move into the next one.

However, this behavior is further complicated by Jersey, its default configuration takes over all static resource serving itself, never allowing Jetty to even have the opportunity to serve those static resources.

What you'll need to do.

  • your MYAPP.war must have the static resources.
  • if you have "external" static resources, those must be served by something in MYAPP.war - either a formal servlet that does it, or something in Jersey. Suggestion is to set that external behavior up at a different url-pattern like /external/* or /static/*, not /*.

Upvotes: 2

Related Questions