Servlet 和映射名称

Servlet and mapping name

你好我是新手,我是 JEE 的新手。 我尝试将我的 servlet class 连接到 web.xml 文件,但我总是遇到此错误:

Servlet should have a mapping name

而且我不知道为什么以及添加映射名称的目的是什么 这是我的 web.xml 文件

<?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_4_0.xsd"
     version="4.0">
<servlet>
    <servlet-name>SelectLiquorServlet</servlet-name>
    <servlet-class>com.sample.SelectLiquorServlet</servlet-class>
</servlet>

首先:Servlet 映射指定 Web 容器,其中 java servlet 应该被客户端给定的 url 调用。它将 url 模式映射到 servlet。当有 request from a client, servlet container 时,决定它应该转发到哪个应用程序。然后 url 的上下文路径匹配映射 servlet。

在维基百科页面上查看第 2 步的第一个项目符号,Java Servlet。 (强调已添加)

The following is a typical user scenario of these methods.

  1. Assume that a user requests to visit a URL.
    • The browser then generates an HTTP request for this URL. This request is then sent to the appropriate server.
  2. The HTTP request is received by the web server and forwarded to the servlet container.
    • The container maps this request to a particular servlet. ⬅
    • The servlet is dynamically retrieved and loaded into the address space of the container.
  3. The container invokes the init() method of the servlet. …

要解决您的问题,您需要添加以下行:

<servlet-mapping>
    <servlet-name>SelectLiquorServlet</servlet-name>
    <url-pattern>/SelectLiquor</url-pattern>
</servlet-mapping>

快乐编码