# Servlet 注解示例 > 原文: [https://javatutorial.net/servlet-annotation-example](https://javatutorial.net/servlet-annotation-example) 本示例演示了注解的用法,以便配置 Servlet。 在之前的教程中,我们使用部署描述符(`web.xml`文件)来配置我们的 servlet。 从 Servlet 3.0 开始,您可以改用`@WebServlet`注解。 该注解使您可以为 servlet 设置几个属性,例如名称,URL 等。 ## 注解与部署描述符 有什么不同? 好吧,很明显,部署描述符是一个单独的文件,您可以在其中以 XML 格式设置配置值,其中注解直接嵌入到源代码中。 如果您希望将代码和配置放在同一位置以提高可读性,请使用注解。 部署描述符恰恰相反 - 您将代码和配置分开。 这样,如果您要更改单个配置值,则无需重新编译整个项目。 对于许多 Java Enterprise 组件,都有两个版本可用 - 注解或描述符。 但其他配置则只能使用注解或通过部署描述符进行配置。 对于 Servlet,可以选择一种或另一种方法。 ## `WebServlet`注解属性 您可以选择几种属性来配置 Servlet。 ### 必要 * `value`或`urlPatterns` `String[]` – 指定该 servlet 的一个或多个 URL 模式。 可以使用任何一个属性,但不能同时使用。`urlPatterns`的默认值为`{}`,`value`默认值为`""` ### 可选 * `asyncSupported` `boolean` – 声明 Servlet 是否支持异步操作模式。 默认值为假 * `name` `String` – Servlet 的名称。 默认值为`""` * `description` `String` – Servlet 的描述。 默认值为`""` * `displayName` `String` – Servlet 的显示名称。 默认值为`""` * `initParams` `WebInitParam[]` – Servlet 的初始化参数。 默认值为`{}` * `largeIcon` `String` – Servlet 的大图标。 默认值为`""` * `smallIcon` `String` – Servlet 的小图标。 默认值为`""` * `loadOnStartup` `int` – Servlet 的启动时加载顺序。 默认值为 -1 ## 用于 Servlet 的 POM 文件 我们将使用以下`pom.xml`文件构建本教程后面显示的示例。 ```java 4.0.0 net.javatutorial.tutorials ServletAnnotation 1 war ServletAnnotation https://javatutorial.net UTF-8 javax.servlet javax.servlet-api 3.1.0 provided servletannotation src/main/java org.apache.maven.plugins maven-war-plugin 2.3 src/main/webapp org.apache.maven.plugins maven-compiler-plugin 3.1 1.8 1.8 ``` ## 使用注解时是否需要`web.xml`? 简短答案–不! 如果您选择对所有 Servlet 配置依赖注解,则可以完全删除`web.xml`文件。 但我仍然鼓励您保留该文件,或者至少保留一个如以下示例所示的文件。`Web.xml`用于许多其他配置,因此您迟早需要在项目中使用它。 ```java Servlet with Annotations Application ``` 如果您希望完全跳过`web.xml`,则需要在 Maven 的`pom.xml`中告诉 war 插件停止搜索该文件。 您可以通过将`failOnMissingWebXml`设置为`false`来实现,如下所示: ```java org.apache.maven.plugins maven-war-plugin 2.3 false ``` ## 用一个 URL 模式注解的 Servlet 以下示例仅使用一个属性来注解 servlet 的 URL。 假设您在本地主机上运行应用程序并将其作为`servletannotation.war`部署,则 servlet 的路径将为`http://localhost:8080/servletannotation/hello` ```java package net.javatutorial.tutorials; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/hello") public class ServletWithAnnotations extends HttpServlet { private static final long serialVersionUID = -3462096228274971485L; @Override protected void doGet(HttpServletRequest reqest, HttpServletResponse response) throws ServletException, IOException { response.getWriter().println("Hello World!"); } } ``` ## 具有多个属性的 Servlet 注解 在此示例中,我们将设置 servlet 名称,URL 和`load-0n-startup`加载优先级。 在我们的[简单 Servlet 示例](https://javatutorial.net/java-servlet-example)中,我们在`web.xml`文件中执行了以下操作: ```java Servlet with Annotations Application simpleServlet net.javatutorial.tutorials.ServletWithAnnotations 1 simpleServlet /hello ``` …现在我们将使用`WebServlet`注解实现相同的配置: ```java package net.javatutorial.tutorials; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet(name = "simpleServlet", urlPatterns = { "/hello" }, loadOnStartup = 1) public class ServletWithAnnotations extends HttpServlet { private static final long serialVersionUID = -3462096228274971485L; @Override protected void doGet(HttpServletRequest reqest, HttpServletResponse response) throws ServletException, IOException { response.getWriter().println("Hello World!"); } } ``` ## 具有多个 URL 模式的 Servlet 注解 `urlPatterns`属性接受一个数组作为值,因此您可以设置多个指向同一 servlet 的 URL: ```java @WebServlet(urlPatterns = {"/hello", "/wellcome"}) public class ServletWithAnnotations extends HttpServlet { @Override protected void doGet(HttpServletRequest reqest, HttpServletResponse response) throws ServletException, IOException { // ... implementation goes here } } ``` 假设您在本地主机上运行应用程序并将其作为`servletannotation.war`部署,则可以访问`http://localhost:8080/servletannotation/hello`以及`http://localhost:8080/wellcome`上的 servlet 您可以在我们的 GitHub 存储库[中找到此示例的源代码](https://github.com/JavaTutorialNetwork/Tutorials/tree/master/ServletAnnotation)