Tomcat 7 的 @WebServlet 注释 [英] @WebServlet annotation with Tomcat 7

查看:32
本文介绍了Tomcat 7 的 @WebServlet 注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我在 web.xml 中定义了一个 servlet:

In my application, I had a servlet which was defined like this in the web.xml:

<servlet>
    <display-name>Notification Servlet</display-name>
    <servlet-name>NotificationServlet</servlet-name>
    <servlet-class>com.XXX.servlet.NotificationServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>NotificationServlet</servlet-name>
    <url-pattern>/notification/*</url-pattern>
</servlet-mapping>

改用 Tomcat 7 后,我想使用 @WebServlet 注释来完成这项工作.
我是这样做的:

After moving to use Tomcat 7, I would like to use the @WebServlet annotation that will do the job.
Here is the way I did it:

@WebServlet( name="NotificationServlet", displayName="Notification Servlet", urlPatterns = {"/notification"}, loadOnStartup=1)
public class NotificationServlet extends HttpServlet {

而且它不起作用.有人能告诉我我做错了什么吗?

And it does not work. Could someone please tell me what I did wrong?

推荐答案

如果您确定使用的是 Tomcat 7 或更新版本,则必须声明 webapp 的 web.xml 符合Servlet 3.0 规范,以便让 Tomcat 扫描和处理注释.否则 Tomcat 仍会以与 web.xml 中的 Servlet 版本匹配的后备模式运行.servlet API 注解支持仅在 Servlet 3.0 (Tomcat 7) 中添加.

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. Otherwise Tomcat will still run in a fallback modus matching the Servlet version in web.xml. The support for servlet API annotations was only added in Servlet 3.0 (Tomcat 7).

因此,web.xml 的根声明必须如下所示(确保您也从 web.xml 中删除了任何 DOCTYPE, 否则仍会被解释为 Servlet 2.3!).

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-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">

此外,URL 模式略有不同.URL 模式 /notifications 将让 servlet 只侦听该路径上的请求.它不会启动带有诸如 /notifications/list 之类的额外路径的请求.URL 模式 /notifications/* 也会让 servlet 侦听带有额外路径信息的请求.

Further, there's a minor difference in the URL pattern. The URL pattern /notifications will let the servlet only listen on requests on exactly that path. It does not kick in on requests with an extra path like /notifications/list or something. The URL pattern /notifications/* will let the servlet listen on requests with extra path info as well.

最小的 @WebServlet 注释应该是这样的

The minimum @WebServlet annotation should thus look like this

@WebServlet("/notifications/*")

其余的属性是可选的,因此不是让 servlet 同等发挥作用所必需的.

The rest of attributes are optional and thus not mandatory to get the servlet to function equally.

这篇关于Tomcat 7 的 @WebServlet 注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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