Java EE 8 : Servlet Mapping Detection

Java EE 8’s Servlet now provides an easy way to detect the URL mapping which invoked the Servlet.
A Servlet can have multiple Servlet Mappings, for example, we can access a Servlet by this mapping “/hi” and this one too “/page.html”. These mappings are usually defined either in the deployment descriptor or via annotations.
Using the deployment descriptor
1 2 3 4 5 6 7 8 9 10 11 | <servlet> <servlet-name>MainServlet</servlet-name> <servlet-class>servlet.MainServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>MainServlet</servlet-name> <url-pattern>/</url-pattern> <url-pattern>/main</url-pattern> <url-pattern>*.html</url-pattern> <url-pattern>/main/*</url-pattern> </servlet-mapping> |
Using annotations
1 2 3 4 | @WebServlet(urlPatterns={"/", "/main", "*.html", "/main/*"}) public class MainServlet extends HttpServlet { .... } |
The new Servlet API now allows us to easily detect which mapping was used in order to invoke the Servlet.
This can be done by retrieving the javax.servlet.http.HttpServletMapping from the received javax.servlet.http.HttpServletRequest and this interface provides 4 methods.
- getMappingMatch
- This is an enum with possible values:
- CONTEXT_ROOT
- This is used when the mapping was achieved with an exact match to the application’s context root
- DEFAULT
- This is used when the mapping was achieved with an exact match to the default servlet of the application, the ‘
/
‘ character
- This is used when the mapping was achieved with an exact match to the default servlet of the application, the ‘
- EXACT
- This is used when the mapping was achieved with an exact match to the incoming request
- EXTENSION
- This is used when the mapping was achieved using an extension, such as “
*.xhtml
“
- This is used when the mapping was achieved using an extension, such as “
- PATH
- This is used when the mapping was achieved using a path, such as “
/faces/*
“
- This is used when the mapping was achieved using a path, such as “
- CONTEXT_ROOT
- This is an enum with possible values:
- getMatchValue
- Return the portion of the URI path that caused this request to be matched
- getPattern
- Returns the pattern that was matched
- getServletName
- Returns the name of the servlet
Now let’s put this to work, create a maven project, add the javax.servlet-api dependency (version >= 4.0) and create a Servlet.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | package servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletMapping; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.MappingMatch; @WebServlet(urlPatterns = { "/", "/main", "*.html", "/main/*" }) public class MainServlet extends HttpServlet { private static final long serialVersionUID = 1 L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //Discover the manner in which this HttpServlet was invoked HttpServletMapping httpServletMapping = req.getHttpServletMapping(); //Retrieve MappingMatch Enum MappingMatch mappingMatch = httpServletMapping.getMappingMatch(); //Print additional results System.out.println("Mapping Match: " + mappingMatch); System.out.println("Match Value: " + httpServletMapping.getMatchValue()); System.out.println("Pattern: " + httpServletMapping.getPattern()); System.out.println("Servlet Name: " + httpServletMapping.getServletName()); } } |
Deploy on the latest GlassFish 5 server and check the results!
- Test #1: http://localhost:8080/
Mapping Match: DEFAULT
Match Value:
Pattern: /
Servlet Name: servlet.MainServlet
- Test #2: http://localhost:8080/main
Mapping Match: EXACT
Match Value: main
Pattern: /main
Servlet Name: servlet.MainServlet
- Test #3: http://localhost:8080/index.html
Mapping Match: EXTENSION
Match Value: index
Pattern: *.html
Servlet Name: servlet.MainServlet
- Test #4: http://localhost:8080/main/hello
Mapping Match: PATH
Match Value: hello
Pattern: /main/*
Servlet Name: servlet.MainServlet