8

I would like to be able to go to

https://localhost:8080/users/pages/profile (without the Server failing) instead of https://localhost:8080/users/pages/profile.html

So I tried

ServletContextHandler pagesContext = new ServletContextHandler();
pagesContext.setContextPath("/users/pages");
ResourceHandler resourceHandler = new ResourceHandler();
resourceHandler.setResourceBase("./src/main/webapp/pages");
resourceHandler.setDirectoriesListed(true);
pagesContext.setHandler(resourceHandler);
pagesContext.addServlet(new ServletHolder("default", DefaultServlet.class), "*.html"); 
// TODO this is not working currently
jettyServer.addHandler(pagesContext);

But it isn't working! It complains that profile page doesn't exist but profile.html works fine

  • 1
    That is not supported by Jetty (or any servlet Java web server for that matter). Any implementation of this support would be highly webapp specific with rules specific to that webapp. The lookup requirements would either need some complex caching (for performance), or frequent filesystem requests (simplifying the implementation, and reducing performance in a non-trivial way) – Joakim Erdfelt Aug 08 '16 at 14:37
  • @JoakimErdfelt since you are a Jetty dev, could you please tell me how to route http requests to https? for example, if the user goes to `http://domain.com` then quickly redirect him to `https://domain.com` –  Aug 09 '16 at 14:14
  • Use [`SecureRedirectHandler`](http://download.eclipse.org/jetty/9.3.11.v20160721/apidocs/org/eclipse/jetty/server/handler/SecuredRedirectHandler.html) - *Tip: be sure you have your HttpConfiguration properly setup.* – Joakim Erdfelt Aug 09 '16 at 18:19

3 Answers3

1

You can do it by url routing. Here is similar question, in which has been explained, how to do it: Jetty '{servlet}/{parameter}' url routing

In nutshell you will only define new servlet. (without or with parameter)

Přemysl Šťastný
  • 1,676
  • 2
  • 18
  • 39
  • I'm using Jersey so I can do that url routing. but how can I do that with static pages?! –  Aug 16 '16 at 09:42
  • Make dynamic page from static page...there will be no result difference... – Přemysl Šťastný Aug 16 '16 at 10:00
  • so you're suggesting I do something like `@Path("users/pages/profile") @Produces("text/html") public Response getProfilePage() { // ??? }` –  Aug 17 '16 at 15:13
  • but what `Response` should I return? I want to load the written html page in my webapp folder. How can I do that? –  Aug 17 '16 at 15:15
  • You can load that HTML into it. See Hello World example: https://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty /by the way I am unhappy you haven't given bounty to anyone/ – Přemysl Šťastný Aug 17 '16 at 16:08
  • oh lol. well your solution was not well elaborate! And I was busy I didn't check SO all this time –  Aug 18 '16 at 09:32
  • I actually still don't get your solution; are you suggesting that I write something like this? `response.getWriter().println("

    hello world

    ");` which is a very horrible idea!

    –  Aug 18 '16 at 11:42
0

Technically speaking, there won't be src folder anymore after packaging the application, In eclipse usually it will all source files will be compiled and moved to classes or build folder, so changing the path from ./src/main/webapp/pages to the correct one may help fixing your issue.

Kiran Kumar
  • 1,033
  • 7
  • 20
0

Jetty wont handle such exception. its entirely upto servlet to do handle such url mapping. Why dont you look at Spring web mvc that will make things a lot more easier to do what you have been asking.

Here's a good place to get started Documentation

Sohil
  • 532
  • 7
  • 26
  • I know about Spring but I simply cannot use it in this project! I thought Jetty handles such url mapping –  Aug 16 '16 at 09:41