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

查看:31
本文介绍了如何在不映射 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

我放在文件夹中:tomcatwebappsROOTWEB-INFclasses

我读过没有必要在 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.

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

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