简体   繁体   中英

default servlet - does this work properly?

I set up some servlets in java. They are identified by the path name. But if a requested one does not exist, I would like to call my "default servlet". I tried to solve it that way (see my code). I tested it successfully, but I'm not sure if it could become instable (eg "servlet one" is forwarded to the default servlet caused by some thread management or something else by the OS...)

Server server = new Server(8080);
WebAppContext context = new WebAppContext();
context.setContextPath("/");
HandlerCollection handlers = new HandlerCollection();
handlers.addHandler(context);
RequestLogHandler requestLogHandler = new RequestLogHandler();
handlers.addHandler(requestLogHandler);
server.setHandler(handlers);

context.setResourceBase("WebContent");
context.addServlet(new ServletHolder(new MyFirstServlet()), "/servlet one/*");
context.addServlet(new ServletHolder(new MySecondServlet()), "/servlet two/*");
context.addServlet(new ServletHolder(new DefaultServlet()), "/*");
context.setInitParameter("dirAllowed", "true");

server.start();

I use java with jetty library.

Thanks for your help.

The Servlet (3.0) specification requires containers to match URL patterns consistently:

  1. The container will try to find an exact match of the path of the request to the path of the servlet. A successful match selects the servlet.
  2. The container will recursively try to match the longest path-prefix. This is done by stepping down the path tree a directory at a time, using the / character as a path separator. The longest match determines the servlet selected.
  3. If the last segment in the URL path contains an extension (eg .jsp ), the servlet container will try to match a servlet that handles requests for the extension. An extension is defined as the part of the last segment after the last . character.
  4. If neither of the previous three rules result in a servlet match, the container will attempt to serve content appropriate for the resource requested. If a "default" servlet is defined for the application, it will be used. Many containers provide an implicit default servlet for serving content.

The container must use case-sensitive string comparisons for matching.

So, the same URL should match the same set of patterns every time.

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