How to map a servlet filter on /* in Jetty?

2019-06-01 04:08发布

问题:

I have a servlet filter which I want to map to http://127.0.0.1:8888/ in Jetty. I have put a servlet filter mapping with url pattern /*. However, the filter is not called. I also tried with / mapping. It does not work either. How is this caused and how can I solve it?

回答1:

If you name your war root.war it will be deployed to the root / context.

http://wiki.eclipse.org/Jetty/Howto/Deploy_Web_Applications



回答2:

You can redirect requests from "/" to "/urlToRedirect" and handle "/urlToRedirect" by special servlet. Like in example:

public static void main(String[] args) throws Exception {
    Servlet frontend = new Frontend();

    Server server = new Server(8080);
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.addServlet(new ServletHolder(frontend), "/index");
    context.addServlet(new ServletHolder(frontend), "/auth");

    ResourceHandler resource_handler = new ResourceHandler();
    resource_handler.setDirectoriesListed(false);
    resource_handler.setResourceBase("static");

    RewriteHandler rewriteHandler = new RewriteHandler();
    rewriteHandler.setRewriteRequestURI(true);
    rewriteHandler.setRewritePathInfo(true);
    rewriteHandler.setOriginalPathAttribute("requestedPath");
    RedirectRegexRule rule = new RedirectRegexRule();
    rule.setRegex("/");
    rule.setReplacement("/index");
    rewriteHandler.addRule(rule);

    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[]{rewriteHandler, resource_handler, context});
    server.setHandler(handlers);

    server.start();
    server.join();
}


回答3:

The Jetty web-app deployment doc mentions that the wep-app can be packaged as root.war to have a root context /



回答4:

Each application in jetty is deployed to its context - meaning it have some part after the slash - i.e http://127.0.0.1:8888/context/ - your servlet mapping is realitve to the last slash - not the one before context

As mentioned in the other answers you must deploy the application as root.war to overcome this.



回答5:

Optionally you could create a directory with name root under $JETTY_HOME/webapps and copy your web app contents into $JETTY_HOME/webapps/root