本文介绍了Tomcat:不附加尾随“/"查找目录时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

访问对应目录的 url 时,应附加 "/" .为了让静态文件(.css、.js ..)正常工作,这是必需的,但是对于 Tomcat 附带的示例站点,这可以工作(例如访问 http://127.0.0.1:8080/examples 突然重新映射到 http://127.0.0.1:8080/examples/),在我的网站上,tomcat 没有添加任何/",从而导致静态文件搜索中断.当手动调用 localhost:8080/myapp/(

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.

应该怎么办?请注意,我没有触及任何 Tomcat 设置,我只是在尝试直接从 Apache 的 zip 文件中得到的内容!

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!

谢谢

推荐答案

首先,您应该详细说明您的情况.正如@mico 所说,这是一个解决方案:

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>

解决方案的想法基于:

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

在这里您可以阅读更多信息:http://jersey.576304.n2.nabble.com/Tomcat-trailing-slashes-and-welcome-files-td4684494.html

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

这里说:如果你有'/*'映射,那么Tomcat不会重定向

还有一点:你应该看看 tomcat 如何记录你的请求并确定主要问题.

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

这篇关于Tomcat:不附加尾随“/"查找目录时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 08:39