Servlet Mapping Using Web.Xml

Servlet Mapping using web.xml

It allows servlets to have multiple servlet mappings:

<servlet>
<servlet-name>Servlet1</servlet-name>
<servlet-path>foo.Servlet</servlet-path>
</servlet>
<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/enroll</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/pay</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/bill</url-pattern>
</servlet-mapping>

It allows filters to be mapped on the particular servlet:

<filter-mapping>
<filter-name>Filter1</filter-name>
<servlet-name>Servlet1</servlet-name>
</filter-mapping>

Your proposal would support neither of them. Note that the web.xml is read and parsed only once during application's startup, not on every HTTP request as you seem to think.

Since Servlet 3.0, there's the @WebServlet annotation which minimizes this boilerplate:

@WebServlet("/enroll")
public class Servlet1 extends HttpServlet {

See also:

  • How do servlets work? Instantiation, sessions, shared variables and multithreading
  • Difference between each instance of servlet and each thread of servlet in servlets?
  • Our Servlets wiki page

servlet mapping and servlet name web.xml

You should add a

<servlet>
<servlet-name>YourServlet</servlet-name>
<servlet-class>org.servlets.YourServlet</servlet-class>
</servlet>
<servlet-mapping>
<url-pattern>/*</url-pattern>
<servlet-name>YourServlet</servlet-name>
</servlet-mapping>

It should not throw an exception, at least you're not telling something else
You could try it via annotations, delete your servlet element from the web.xml and add

@WebServlet(servletName="YourServlet", urlPatterns={"/*"})
public class YourServlet extends HttpServlet{
//....
}

How are Servlet url mappings in web.xml used?

From Servlet 3.0 specification, this is how the web container must locate the servlet after receiving a request (emphasis mine):

The path used for mapping to a servlet is the request URL from the
request object minus the context path and the path parameters. The
URL path mapping rules below are used in order. The first successful
match is used with no further matches attempted
:

  1. The container will try to find an exact match of the path of the request to the path of the servlet. A successful match selects the
    servlet.
  2. The container will recursively try to match the longest path-prefix. This is done by stepping down the path tree a directory
    at a time, using the ’/’ character as a path separator. The longest
    match determines the servlet selected.
  3. If the last segment in the URL path contains an extension (e.g. .jsp), the servlet container will try to match a servlet that handles
    requests for the extension. An extension is defined as the part of
    the last segment after the last ’.’ character.
  4. If neither of the previous three rules result in a servlet match, the container will attempt to serve content appropriate for the
    resource requested. If a "default" servlet is defined for the
    application, it will be used. Many containers provide an implicit
    default servlet for serving content.

The container must use case-sensitive string comparisons for matching.

You should also look the specification of mappings (given below):

In the Web application deployment descriptor, the following syntax is
used to define mappings:

  • A string beginning with a ‘/’ character and ending with a ‘/*’ suffix
    is used for path mapping.

  • A string beginning with a ‘*.’ prefix is used as an extension mapping.

  • The empty string ("") is a special URL pattern that exactly maps to
    the application's context root, i.e., requests of the form
    http://host:port/<contextroot>/. In this case the path info is ’/’ and
    the servlet path and context path is empty string (““).

  • A string containing only the ’/’ character indicates the "default"
    servlet of the application. In this case the servlet path is the
    request URI minus the context path and the path info is null.

  • All other strings are used for exact matches only

Let us look at examples now. Consider the following set of mappings:


Path Pattern Servlet
/foo/bar/* servlet1
/baz/* servlet2
/catalog servlet3
*.bop servlet4

The following behavior would result:


Incoming Path Servlet Handling Request
/foo/bar/index.html servlet1
/foo/bar/index.bop servlet1
/baz servlet2
/baz/index.html servlet2
/catalog servlet3
/catalog/index.html “default” servlet
/catalog/racecar.bop servlet4
/index.bop servlet4

Note that in the case of /catalog/index.html and /catalog/racecar.bop, the
servlet mapped to “/catalog” is not used because the match is not exact.

Now coming to your problem :)

/path/test belongs to the 5th point of specification of mappings. Which means only the paths ending in /path/test will target servlet1.

However /path/test/* qualifies for the first point of the same specification. This means that:

.../path/test will be handled by servlet1 and

.../path/test/abc will be handled by servlet2

Which was verified by me in a test application.

Mapping servlet in web.xml

You are missing <servlet>...</servlet> tag which is important to mapping. So use following :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>EmployeeManagement</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>name</param-name>
<param-value>Pramod</param-value>
</context-param>
<servlet>
<servlet-name>Registration</servlet-name>
<servlet-class>com.yourPackageName.yourServletName</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Registration</servlet-name>
<url-pattern>/EmployeeManagement/WebContent/Registration</url-pattern>
</servlet-mapping>
</web-app>

and you should give action value on your form like following:

<form action="/EmployeeManagement/WebContent/Registration" method="post">

//Some code here

</form>

and also note it down all values are case sensitive on the following code:

<servlet>
<servlet-name>Registration</servlet-name>
<servlet-class>com.yourPackageName.yourServletName</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Registration</servlet-name>
<url-pattern>/EmployeeManagement/WebContent/Registration</url-pattern>
</servlet-mapping>

Your servlet name Registration should be same on both tags <servlet>...</servlet> and <servlet-mapping>...</servlet-mapping> and also package name should be same where your servlet class is located.

servlet web.xml mapping

Every <servlet> needs to have a unique <servlet-name>, so you will need to rename one. Also, the <url-pattern>'s should be different so the servlet container will know how to handle the requests.

web.xml: how to map domain/xx/* to a servlet?

You can have more than one url-pattern entries:

<servlet>
<servlet-name>NAME</servlet-name>
<servlet-class>MyClass</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>NAME</servlet-name>
<url-pattern>/aa/*</url-pattern>
<url-pattern>/ab/*</url-pattern>
<url-pattern>/ac/*</url-pattern>
</servlet-mapping>

Unfortunately, the servlet mapping cannot take a regular expression so you can't do a variation of /a*/* for the mapping.



Related Topics



Leave a reply



Submit