部署描述符问题

发布于 2024-10-25 15:01:11 字数 941 浏览 2 评论 0原文

我正在尝试将应用程序加载到本地 tomcat 实例。当我在启动 tomcat 后将地址输入到 URL 栏中时,即使资源在那里,我也会收到 404 未找到错误。我做了很多尝试和错误,并在 DD 文件中找到了一些东西。部署描述符的一部分包含以下代码。

<servlet>
    <servlet-name>invoker</servlet-name>
    <servlet-class>
        org.apache.catalina.servlets.InvokerServlet
    </servlet-class>
    <init-param>
        <param-name>debug</param-name>
        <param-value>0</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>invoker</servlet-name>
    <url-pattern>/servlet/*</url-pattern>
</servlet-mapping>

当我从 web.xml 文件中注释这部分时,我能够成功地从浏览器获取资源。所以我很确定问题出在这段代码中,但我无法理解它是什么。有人可以解释一下这段代码在做什么以及为什么我会遇到错误吗?提前致谢。

编辑:根据记录,我试图直接访问根文件夹下的资源。 .. http://localhost/myapp/index.jsp

I am trying to load an application to my local tomcat instance. When I input the address into the URL bar after starting tomcat I'm getting 404-not found error even though the resources were there. I did a lot of trial and error and found something in the DD file. A portion of the deployment descriptor contains following code..

<servlet>
    <servlet-name>invoker</servlet-name>
    <servlet-class>
        org.apache.catalina.servlets.InvokerServlet
    </servlet-class>
    <init-param>
        <param-name>debug</param-name>
        <param-value>0</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>invoker</servlet-name>
    <url-pattern>/servlet/*</url-pattern>
</servlet-mapping>

When I comment this part from the web.xml file I was able to successfully get to the resources from the browser. So I'm pretty sure that the problem is in this code but I can't understand what it is. Can someone please explain what this code is doing and why am I getting error with this.? Thanks in advance.

Edit: For the record, I was trying to access resources directly under root folder. .. http://localhost/myapp/index.jsp

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

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

发布评论

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

评论(1

小巷里的女流氓 2024-11-01 15:01:11

这是旧版 Tomcat 特定的 InvokerServlet 它存在于 Apache Tomcat 的古老版本中(并且在糟糕且过时的教程/书籍中仍然提到)。在黑暗时代,人们可以调用 servlet,而无需将它们映射到 web.xml 中。您只需在 URL 中的 /servlet 路径后输入完全限定的 servlet 类名。

后来证实这是一个安全漏洞并且容易受到攻击。它在 2002 年左右的 Tomcat 5.0 中被禁用和弃用(!),并在 2009 年左右的 Tomcat 7.0 中被删除。

您可以安全地删除它。它没有增加任何价值。从 Tomcat 7.0 / Servlet 3.0 开始,您甚至不再需要在 web.xml 中映射 servlet。您可以使用 @WebServlet 注释代替。

package com.example;

@WebServlet(urlPatterns = { "/MyServlet" })
public class MyServlet extends HttpServlet {

    // ...

}

至于删除它后它为何有效的具体问题,您可能使用了错误的 URL。也许您也将上下文根路径称为 servlet

That's the legacy Tomcat-specific InvokerServlet which was present in ancient versions of Apache Tomcat (and still mentioned in poor and outdated tutorials/books). It was in the dark ages used to be able to invoke servlets without the need to map them in web.xml. You just have to enter the fully qualified servlet classname in the URL after the /servlet path.

It was later confirmed that it was a security hole and vulrenable to attacks. It was disabled and deprecated on Tomcat 5.0 around 2002(!) and removed in Tomcat 7.0 around 2009.

You can safely remove it. It adds no utter value. Since Tomcat 7.0 / Servlet 3.0 you even don't need to map servlets in web.xml anymore. You can use the @WebServlet annotation instead.

package com.example;

@WebServlet(urlPatterns = { "/MyServlet" })
public class MyServlet extends HttpServlet {

    // ...

}

As to the concrete problem why it works after you removed it, you'll probably have used the wrong URLs. Maybe you've called your context root path also servlet?

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