tomcat - 为什么目录升级对图像不起作用?

tomcat - Why doesn't directory level up work for images?

我正在使用 Eclipse 和 Apache Tomcat,我在以下路径中创建了一个名为 SampleTest 的 动态 Web 项目-:

G:\eclipse_workspace\SampleTest

在 SampleTest 目录中有以下文件夹-:

.settings
build
imgs
src
WebContent
.classpath
.project 

我在 Eclipse 中创建了 imgs 文件夹,其他文件夹由 IDE.

放在那里

imgs 文件夹将包含应用程序将使用的图像。我的 index.html 中有一个简单的 <img> 标签(它位于 WebContent 目录中),如下所示:

<img alt="" src="../imgs/img.jpg" width="100" height="100">

如您所见,我已专门指示 Web 服务器 "go up" 来自 WebContent 的目录,以便它可以找到 imgs 文件夹。

这行不通。当我 运行 Eclipse 中的项目时,没有图像显示。但是,如果我将 imgs 保留在 WebContent 文件夹中并使用 /imgs/img.jpg 代替,一切正常。

为什么 Tomcat 不能直接上一级目录?不允许吗?

Why can't Tomcat just go up a directory ? Is it not allowed to ?

正确,因为有一个安全限制旨在防止攻击者复制您的整个文件系统。但是,没有部署该特定图像文件夹,而是相对于 WebContent 提供所有内容(使其成为 "root"),因此您无法进入目录(即 "above"根 如果您不将 imgs 文件夹放在 WebContent 下,则 imgs 文件夹不是 Web 部署的一部分。