Tomcat:不附加尾随“/”查找目录时

发布于 2024-12-11 08:56:46 字数 559 浏览 0 评论 0 原文

我在 Tomcat 7.0.22(Java 1.6、MacOS Lion)中部署了 .war 存档。这场战争被称为“myapp.war”,因此 Tomcat 正在服务 http://localhost/myapp (已将战争扩大到/webapps/myapp)。这是通常的行为

,当访问对应于目录的 url 时,应该附加“/”。为了使静态文件(.css、.js ..)正常工作,这是必需的,但是对于 Tomcat 附带的示例站点来说,这是可行的(例如访问 http://127.0.0.1:8080/examples 突然重新映射到 http://127.0.0.1:8080/examples/),在我的网站上,tomcat 没有添加任何“/”来搜索静态文件 破碎的。当手动调用 localhost:8080/myapp/ 时(<-- 我添加斜杠),一切正常。

应该发生什么事?请注意,我没有触及任何 Tomcat 设置,我只是尝试直接从 Apache 的 zip 文件中获得的内容!

谢谢

I deployed a .war archive in Tomcat 7.0.22 (Java 1.6, MacOS Lion). The war is called "myapp.war" and so Tomcat is serving http://localhost/myapp (having expanded the war in /webapps/myapp). This is the usual behavious

When accessing a url that corresponds to a directory, it should append "/" . For having static files (.css, .js..) working correctly, this is needed, but while with the sample sites that ship with Tomcat this works (for example visiting http://127.0.0.1:8080/examples suddenly remaps to http://127.0.0.1:8080/examples/), on my site tomcat doesn't add any "/" making the search for static files broken. When manually calling localhost:8080/myapp/ (<-- I add the slash) everything works fine.

What should be going on? Mind that I haven't touched any Tomcat setting, I'm just experimenting with what comes out directly from Apache's zip file!

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怎言笑 2024-12-18 08:56:46

首先你应该更多地解释你的情况。正如 @mico 在这里所说的是一个解决方案:

<filter>
    <filter-name>Jersey Filter</filter-name>
     <filter-class>com.sun.jersey.spi.container.servlet.ServletContainer</filter-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>your package name(s)</param-value>
        </init-param>
    <init-param>
            <param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
            <param-value>.*\.jsp|.*\.css|.*\.png|.*\.js|.*\.gif|.*\.jpg</param-value>
        </init-param>
</filter>

<filter-mapping>
    <filter-name>Jersey Filter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
</filter-mapping>

 <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

解决方案的想法基于:

将 Jersey ServletContainer 定义为过滤器而不是 servlet

您可以在此处阅读更多内容: http://jersey.576304.n2.nabble.com/Tomcat-trailing-slashes-and-welcome-files-td4684494.html

这里表示:如果您有“/*”映射,那么 Tomcat 不会重定向

再一个:您应该看到 tomcat 如何记录您的请求并确定主要问题。

Firstly you should explain more about your situation. As @mico has told here is a solution:

<filter>
    <filter-name>Jersey Filter</filter-name>
     <filter-class>com.sun.jersey.spi.container.servlet.ServletContainer</filter-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>your package name(s)</param-value>
        </init-param>
    <init-param>
            <param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
            <param-value>.*\.jsp|.*\.css|.*\.png|.*\.js|.*\.gif|.*\.jpg</param-value>
        </init-param>
</filter>

<filter-mapping>
    <filter-name>Jersey Filter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
</filter-mapping>

 <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

The idea on solution based on:

defining Jersey ServletContainer as a filter instead of a servlet

Here you can read more: http://jersey.576304.n2.nabble.com/Tomcat-trailing-slashes-and-welcome-files-td4684494.html

Here says that: If you have a '/*' mapping, then Tomcat won't redirect

One more: you should see how tomcat logs your request and decide the main problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文