gpt4 book ai didi

Java Servlet - 将 servlet 映射到除字符串之外的每个 URL

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:52:40 25 4
gpt4 key购买 nike

我有一个配置为处理所有 URL 的 servlet (*):

<servlet>
<servlet-name>MyServ</servlet-name>
<servlet-class>MyServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>MyServ</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

我需要以 /static/ 开头的 URL,它应该从静态 WEB-INF 为它们提供服务。也就是说,MyServ 应该为 /static 之外的所有内容提供服务。

我该怎么做?


更新:澄清一下,我想要的是:

/*/ - 转到 MyServ
/static/dir/file.css - Jetty 提供来自/dir/的静态 file.css。

我不确定要做什么 web.xml,或者将静态文件放在哪里。

我尝试添加这个:

<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/static/*</url-pattern>
</servlet-mapping>

但是,当我转到 /static/ URL 时,我得到:

HTTP ERROR 404

Problem accessing /static/dir/file.css. Reason:
Not Found

Powered by Jetty://

我不确定我的 web.xml 是否有误,或者我只是将文件放在错误的位置(我已经在 src/main/webappsrc/main/webapp/lib/META-INF/resources/)


jetty

我正在使用 Jetty。我想避免任何其他层,例如 Nginx、Apache 等。

要赢得赏金,请确保您回答 Jetty 的作品。

最佳答案

最好的办法可能是在 * 规则之前有一个 static 规则。

Rule for URL path mapping:

It is used in the following order. First successful match is used with no further attempts.

  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.

所以它会匹配/static/的规则,然后到此为止。

关于Java Servlet - 将 servlet 映射到除字符串之外的每个 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27432060/

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