0

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

2 Answers 2

4

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.
Sign up to request clarification or add additional context in comments.

Comments

0

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"
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.