NotchxY
NotchxY

Reputation: 23

Servlet and mapping name

Hello I am a newbie and I am new at JEE. I try to connect my servlet class to the web.xml file but I always have this error:

Servlet should have a mapping name

and I don't know why and what is the purpose of adding a mapping name Here is my web.xml file

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

Upvotes: 2

Views: 824

Answers (1)

Arbani Oumaima
Arbani Oumaima

Reputation: 366

First : Servlet mapping specifies the web container of which java servlet should be invoked for a url given by client. It maps url patterns to servlets. When there is a request from a client, servlet container decides to which application it should forward to. Then context path of url is matched for mapping servlets.

See first bullet of step # 2 on Wikipedia page, Java Servlet. (Emphasis added)

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. …

To solve your problem you need to add the following lines:

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

Happy Coding

Upvotes: 3

Related Questions