简体   繁体   English

Tomcat:Servlet映射与WebServlet批注

[英]Tomcat: Servlet Mapping vs. WebServlet Annotation

There are two ways for servlet mapping. servlet映射有两种方法。 The first is in web.xml: 第一个是在web.xml中:

<servlet>
   <servlet-name>foo</servlet-name>
   <servlet-class>com.whatever.foo</servlet-class>
</servlet>
<servlet-mapping>
   <servlet-name>foo</servlet-name>
   <url-pattern>/foo</url-pattern>
</servlet-mapping>

The second method uses the WebServlet annotation: 第二种方法使用WebServlet批注:

@WebServlet("/foo")
public class foo extends HttpServlet {
...
}

Which one is better ? 哪一个更好 Where are the advantages of the first and the second way? 第一种和第二种方式的优势在哪里?

Provided that you're sure that you're using Tomcat 7 or newer , the webapp's web.xml has to be declared conform Servlet 3.0 spec in order to get Tomcat to scan and process the annotations. 如果您确定使用的是Tomcat 7或更高版本 ,则必须将webapp的web.xml声明为符合Servlet 3.0规范,以便让Tomcat扫描并处理注释。 Otherwise Tomcat will still run in a fallback modus matching the Servlet version in web.xml . 否则,Tomcat仍将以与web.xml的Servlet版本匹配的后备模式运行。 The support for servlet API annotations was only added in Servlet 3.0 (Tomcat 7). Servlet API注释的支持仅在Servlet 3.0(Tomcat 7)中添加。

So, the root declaration of your web.xml must look like below (make sure you remove any DOCTYPE from web.xml too, otherwise it will still be interpreted as Servlet 2.3!). 因此, web.xml的根声明必须如下所示(确保从web.xml删除任何DOCTYPE ,否则它仍将被解释为Servlet 2.3!)。

<web-app 
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

Further, there's a minor difference in the URL pattern. 此外,URL模式存在细微差别。 The URL pattern /notifications will let the servlet only listen on requests on exactly that path. URL模式/notifications将使servlet仅在该路径上侦听请求。 It does not kick in on requests with an extra path like /notifications/list or something. 它没有使用/notifications/list等额外路径启动请求。 The URL pattern /notifications/* will let the servlet listen on requests with extra path info as well. URL模式/notifications/*将让servlet监听具有额外路径信息的请求。

The minimum @WebServlet annotation should thus look like this 因此,最小的@WebServlet注释应如下所示

@WebServlet("/notifications/*")

The rest of attributes are optional and thus not mandatory to get the servlet to function equally. 其余属性是可选的,因此不一定要使servlet平等运行。

What is benefit of using java based config instead of web.xml for servlet 3.x? 对于servlet 3.x,使用基于java的配置而不是web.xml有什么好处? It avoids repeating yourself, and making mistakes by doing so. 它避免重复自己,并通过这样做犯错误。 The servlet class is, for example, com.foo.bar.SomeServlet . 例如,servlet类是com.foo.bar.SomeServlet Using web.xml , you're forced to re-enter this class in web.xml : 使用web.xml ,您不得不在web.xml重新输入此类:

<servlet-class>com.foo.bar.Someservlet</servlet-class>

But wait, you've made a typo and you'll only discover it at runtime. 但是等等,你做了一个拼写错误,你只会在运行时发现它。

Or you rename a servlet class, but you forget to rename it in the web.xml as well, and you only discover the mistake at deployment time. 或者您重命名了一个servlet类,但是您忘记在web.xml中重命名它,并且您只在部署时发现错误。

Finally, they make our life easier. 最后,它们让我们的生活更轻松。 You're creating a servlet, and you obviously want to map it to some URL. 您正在创建一个servlet,显然您希望将其映射到某个URL。 So you just add an annotation. 所以你只需添加一个注释。 No need to go to another file to add the mapping, then go back to the class because you forgot its exact name, then go back to the file again. 无需转到另一个文件来添加映射,然后返回到该类,因为您忘记了它的确切名称,然后再次返回该文件。 Everything regarding a servlet is in the servlet class. 有关servlet的所有内容都在servlet类中。 Same for a filter, listener, etc. 对于过滤器,监听器等也是如此

Annotations don't have all these problems. 注释没有所有这些问题。

I hope this helps you! 我希望这可以帮助你!

XML configuration : XML配置:

advantages : 好处 :

All mappings are in the same location, you have an overview of all of them in one single file. 所有映射都位于同一位置,您可以在一个文件中概览所有映射。

disadvantages : 缺点:

Needs a separate file in addition to the class files. 除了类文件之外,还需要一个单独的文件。

Annotation configuration : 注释配置:

advantages : 好处 :

The mapping is described directly inside the relevant class. 映射直接在相关类中描述。

disadvantages : 缺点:

You have to open a particular class to see its mappings. 您必须打开一个特定的类才能查看其映射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM