如何在web.xml中没有映射的情况下调用servlet? [英] How to invoke a servlet without mapping in web.xml?

查看:288
本文介绍了如何在web.xml中没有映射的情况下调用servlet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用以下URL调用简单的servlet:
http:// localhost:8080 / servlet / MyServlet

How to invoke a simple servlet using the following URL: http://localhost:8080/servlet/MyServlet

我把它放在文件夹中: tomcat\webapps \ROOT \WEB-INF \classes

I placed it in the folder: tomcat\webapps\ROOT\WEB-INF\classes

我读过没有必要在web.xml中提及servlet。我做了同样的事。不过,我无法调用它。

I've read there is no need to mention the servlet in web.xml. I did the same. Still, I'm unable to invoke it.

推荐答案


我在那里读过无需在web.xml中提及servlet。

您可能会对传统的Tomcat内置程序造成混淆 InvokerServlet ,它存在于旧版本的Apache Tomcat中(在穷人和过时的教程/书籍中仍然提到)。它确实允许调用这样的servlet而不需要映射任何东西。但是,后来证实它是一个安全漏洞,并且可以攻击 。它在Tomcat 5.0上被禁用和弃用,并在Tomcat 7.0上被删除。在这种情况下,您确实需要在 web.xml 中映射您的servlet(并将其放在一个包中!)。

You're probably confusing with the legacy Tomcat-builtin InvokerServlet which was present in older versions of Apache Tomcat (and still mentioned in poor and outdated tutorials/books). It indeed allowed to invoke servlets like that without the need to map anything. However, it was later confirmed that it was a security hole and vulrenable to attacks. It was disabled and deprecated on Tomcat 5.0 and removed on Tomcat 7.0. In such case, you really need to map your servlet in web.xml (and put it in a package!).

另一个混乱的来源可能是新的Servlet 3.0 @WebServlet 注释。当您已经使用像Tomcat 7.0这样的Servlet 3.0容器时,您可以使用此批注来映射servlet,而无需使用 web.xml

Another source of confusion may be the new Servlet 3.0 @WebServlet annotation. When you're already using a Servlet 3.0 container like Tomcat 7.0, then you could use this annotation to map the servlet without the need to fiddle with web.xml.

package com.example;

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

    // ...

}

然后您就可以按照自己的方式访问它了。

Then you'll be able to access it the way you want.

  • Our Servlets wiki page

这篇关于如何在web.xml中没有映射的情况下调用servlet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆