gpt4 book ai didi

java - 默认 servlet - 这可以正常工作吗?

转载 作者:行者123 更新时间:2023-12-01 04:54:01 26 4
gpt4 key购买 nike

我在java中设置了一些servlet。它们由路径名标识。但如果所请求的 servlet 不存在,我想调用我的“默认 servlet”。我尝试以这种方式解决它(请参阅我的代码)。我成功地测试了它,但我不确定它是否会变得不稳定(例如,由于某些线程管理或操作系统的其他原因,“servlet one”被转发到默认 servlet...)

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();

我使用 java 和 jetty 库。

感谢您的帮助。

最佳答案

Servlet (3.0) specification要求容器一致地匹配 URL 模式:

  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 (e.g. .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.

因此,相同的 URL 每次都应该匹配相同的模式集。

关于java - 默认 servlet - 这可以正常工作吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14475469/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com