@WebServlet标注使用Tomcat 7 [英] @WebServlet annotation with Tomcat 7

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

问题描述

我在我的应用我这是在这样定义一个servlet的web.xml 的:

I 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或更高版本,web应用程序的的web.xml 已被宣布符合的Servlet 3.0规范,以获得Tomcat能够扫描和处理的注释。否则,Tomcat会仍然处于备用作案匹配的web.xml Servlet的版本上运行。仅在3.0的Servlet(Tomcat 7的)增加了对Servlet API的注释的支持。

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的根声明必须看起来像下面(请确保您删除任何 DOCTYPE 的web.xml 太多,否则仍然会作为PTED 2.3的Servlet!)间$ p $。

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模式 /通知将让这个servlet只上正是路径上请求听。它不会踢的请求像 /通知/列表或一些额外的路径。 URL模式 /通知/ * 将让这个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.

  • Our servlets wiki page

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

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