Java Servlet – 将servlet映射到每个URL但字符串

前端之家收集整理的这篇文章主要介绍了Java Servlet – 将servlet映射到每个URL但字符串前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个servlet配置为处理所有URL(*):
<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 /开头的URLS,它应该从静态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>

但是,当我去/静态/ URL,我只是得到:

HTTP ERROR 404

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

Powered by Jetty://

我不知道我的web.xml是否错误,或者我只是将文件放在错误的地方(我已经在src / main / webapp和src / main / webapp / lib / Meta-INF /资源/)

码头

我正在使用码头.我想避免任何其他层,如Nginx,Apache等.

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

解决方法

你最好的赌注可能是在*的规则之前有一个静态规则.

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 /的规则,并停止在那里.

原文链接:https://www.f2er.com/java/126285.html

猜你在找的Java相关文章