SwiftMango
SwiftMango

Reputation: 15284

Servlet URL pattern match with slash prefix

I have the servlet annotation as:

@WebServlet (name="PageServlet", urlPatterns={"*.zt"})

zt is the file extension I am using.

However if I use

@WebServlet (name="PageServlet", urlPatterns={"/*.zt"})

It does not match the URL such as /app/index.zt , nor /app/foo/test.zt... How can I match only the zt files under root /app/?

Upvotes: 0

Views: 2202

Answers (2)

atish shimpi
atish shimpi

Reputation: 5023

In your code * is not treated as wild-card, you have to use /app/*

Example 1 : /string1/*/string2 , * will work as wild-card

@WebServlet(
urlPatterns={"/string1/*/string2"} ,
name="ServletName",
initParams={ @WebInitParam(name = "name", value = "string2")
} )

Example 2 :

@WebServlet(
urlPatterns={"/string1/*"} ,
name="ServletName"
)

Upvotes: 0

Steve C
Steve C

Reputation: 19445

The servlet container either uses path mapping or extension mapping. You cannot use a combination of the two. See ¶12.2 of the Servlet Specification:

  • 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/<context- root>/. 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.

Upvotes: 4

Related Questions