3

I am using Jetty-9 in embedded mode and need only one web application. Consequently I would like the root URL to go to the homepage of that application, i.e. something like

http://localhost:4444/

should end up in a servlet. I start out with:

ServletContextHandler scContext = 
        new ServletContextHandler(ServletContextHandler.SESSIONS);
scContext.setContextPath("/");

None of the following worked, neither

scContext.addServlet(ListsServlet.class, "/");

nor

scContext.setWelcomeFiles(new String[]{"/lists})

where /lists is mapped to the ListsServlet servlet. All I get is a 403 (Forbidden).

I do not use the DefaultServlet, which seems to handle welcome files. But since the ServletContextHandler has setWelcomeFiles I expected it to contain the logic to use them.

Any ideas?

Harald
  • 4,575
  • 5
  • 33
  • 72

1 Answers1

3

For the 403 Forbidden error, you have some security setup that is not allowing you to access the handlers/servlets.

Eliminate that security (for now), verify that the rest is working, then add security a bit later to lock down specifics.

If you want to see some the suggestions below at work, consider looking at the code example in the answer from another stackoverflow: How to correctly support html5 <video> sources with jetty.

Welcome files are appended to the incoming request path if there is nothing present at that location. For example requesting a directory and then a welcome-file of 'index.html' is appended to the request path.

While this would work ...

scContext.setWelcomeFiles(new String[]{"lists"})

// Add Default Servlet (must be named "default")
ServletHolder holderDefault = new ServletHolder("default",DefaultServlet.class);
holderDefault.setInitParameter("resourceBase",baseDir.getAbsolutePath());
holderDefault.setInitParameter("dirAllowed","true");
holderDefault.setInitParameter("welcomeServlets","true");
holderDefault.setInitParameter("redirectWelcome","true");

scContext.addServlet(holderDefault,"/");

It's likely not what you are aiming for, as you said the root path only. The above would also make changes to requests like /foo/ to /foo/lists

Instead, it might make more sense to use a Rewrite rule + handler instead of the welcome-files approach.

RewriteHandler rewrite = new RewriteHandler();
rewrite.setHandler(scContext);

RewritePatternRule rootRule = new RewritePatternRule();
rootRule.setPattern("/");
rootRule.setReplacement("/list");
rootRule.setTerminating(true);
rewrite.addRule(rootRule);

server.setHandler(rewrite);

This RewritePatternRule simply changes any request path / to /list and then forwards that request to the wrapped ssContext (if you want to see the /list on the browser, change it to a RedirectPatternRule instead.

Community
  • 1
  • 1
Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
  • Cool, this should work, except I cannot currently verify it, because whatever I do, I get "Problem accessing /. Reason: Forbidden". Need to research how to get around it. – Harald Oct 09 '14 at 06:15
  • Wow, that was time consuming. I had a `ResourceHandler` in front of the `HandlerList` and it freaks out with "Forbidden" when it sees "/". Wrapping the `RewriteHandler` also around it helped, but I could not use a `RedirectRule` for `"/"` to `"/lists"`, because this creates a redirect loop. What works is a `RegexRedirectRule` for `^/?$`. Thanks anyway, you gave just the right hint. – Harald Oct 10 '14 at 06:51
  • @Harald I found that `RedirectPatternRule.setPattern( "" )`, as opposed to `( "/" )`, prevents the looping. – Michael Allan Oct 20 '14 at 04:26
  • The first approach with welcomeServlets works like a charm. Thank you! – Fabiano Tarlao Apr 30 '18 at 09:23