In the servlet specification, you have no concept of path variables. Some MVC environments support them, such as Struts or Spring MVC.
For the servlet point, the URL is:
scheme:
where any of the parts (starting from the context path may be zero)
Specification for Servlet 3.0:
- Context Path: The path prefix associated with the ServletContext that the Servlet is part of. If this context is a βstandardβ context, rooted in the web server namespace, this path will be an empty string. Otherwise, if the context is not rooted in the root namespace of the servers, the path starts with / but does not end with the / character.
- Servlet path: a section of the path that directly corresponds to the mapping that activated this request. This path starts with the character /, unless the request matches the pattern "/ * or", in which case it is an empty string.
- PathInfo: part of the request path that is not part of the Context path or Servlet path. This is either null if there is no additional path, or a line with a leading "/.
In the HttpServletRequest interface, the following methods exist for accessing this information:
- getContextPath
- getServletPath
- getPathInfo
It is important to note that, with the exception of differences in URL encoding between the URI request and parts of the path, the following equation is always true:
requestURI = contextPath + servletPath + pathInfo
This means that you just need to use @WebServlet(urlPatterns={"/posts"}) and then decode the pathInfo part with your hands to retrieve commands and parameters
source share