Reputation: 53
I need to implement 3 servlets, with pattern overlapping.
The first servlet would handle for example:
/context/categories
with a pattern like this:
@WebServlet("/categories"))
The second servlet would handle for example:
/context/categories/category1/
with a pattern like this:
@WebServlet("/categories/*")
And the third would be like:
/context/categories/category1/category1contentname
But i cant give the proper pattern to the third, because the second servlet will catch the call, or neither will. My question is, how can i give the correct patterns to the servlets, especially the second and the third? I know the /* suffix, and the *. prefix, but it doesnt seem to work properly so far.
Upvotes: 1
Views: 1412
Reputation: 280112
I will assume that with
/context/categories/category1/
you mean that the /category1
path segment can have any value.
Basically, you can't with Servlet url-pattern
elements. The Servlet Specification says the following about path matching:
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:
- 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.
- 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.
- [...]
- [...]
The mapping
@WebServlet("/categories/*")
will match everything that starts with /categories
.
As I see it, you don't have many solutions. Declare a single Servlet
with the pattern /categories/*
and internally do some dispatching. You should look into the Front Controller pattern.
Upvotes: 1