<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 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 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>net.javatutorial.tutorials</groupId> <artifactId>ServletAnnotation</artifactId> <version>1</version> <packaging>war</packaging> <name>ServletAnnotation</name> <url>https://javatutorial.net</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> </dependencies> <build> <finalName>servletannotation</finalName> <sourceDirectory>src/main/java</sourceDirectory> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.3</version> <configuration> <warSourceDirectory>src/main/webapp</warSourceDirectory> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project> ``` ## 使用注解時是否需要`web.xml`? 簡短答案–不! 如果您選擇對所有 Servlet 配置依賴注解,則可以完全刪除`web.xml`文件。 但我仍然鼓勵您保留該文件,或者至少保留一個如以下示例所示的文件。`Web.xml`用于許多其他配置,因此您遲早需要在項目中使用它。 ```java <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>Servlet with Annotations Application</display-name> </web-app> ``` 如果您希望完全跳過`web.xml`,則需要在 Maven 的`pom.xml`中告訴 war 插件停止搜索該文件。 您可以通過將`failOnMissingWebXml`設置為`false`來實現,如下所示: ```java <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.3</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> ``` ## 用一個 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 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>Servlet with Annotations Application</display-name> <servlet> <servlet-name>simpleServlet</servlet-name> <servlet-class>net.javatutorial.tutorials.ServletWithAnnotations</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>simpleServlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app> ``` …現在我們將使用`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)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看