java - How are Servlet url mappings in web.xml used? -
i have web.xml file content:
<servlet> <servlet-name>servlet1</servlet-name> <servlet-class>org.mycompany.test1</servlet-class> </servlet> <servlet> <servlet-name>servlet2</servlet-name> <servlet-class>org.mycompany.test2</servlet-class> </servlet> <servlet-mapping> <servlet-name>servlet1</servlet-name> <url-pattern>/path/test</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>servlet2</servlet-name> <url-pattern>/path/test/*</url-pattern> </servlet-mapping> i tried requests
.../path/test/abc .../path/test both requests processed servlet2. why?
update
thank guys help. realised behaviour depends on order of servlet-mapping declaration. tried web.xml
<servlet> <servlet-name>servlet1</servlet-name> <servlet-class>org.mycompany.test1</servlet-class> </servlet> <servlet> <servlet-name>servlet2</servlet-name> <servlet-class>org.mycompany.test2</servlet-class> </servlet> <servlet> <servlet-name>servlet3</servlet-name> <servlet-class>org.mycompany.test3</servlet-class> </servlet> <servlet> <servlet-name>servlet4</servlet-name> <servlet-class>org.mycompany.test4</servlet-class> </servlet> <servlet-mapping> <servlet-name>servlet1</servlet-name> <url-pattern>/path/test</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>servlet2</servlet-name> <url-pattern>/path/test/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>servlet3</servlet-name> <url-pattern>/path/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>servlet4</servlet-name> <url-pattern>/path</url-pattern> </servlet-mapping> results:
.../path/test/abc - servlet2 .../path/test/ - servlet2 .../path/test - servlet2 .../path/abc - servlet3 .../path/ - servlet4 .../path - servlet4
from servlet 3.0 specification, how web container must locate servlet after receiving request (emphasis mine):
the path used mapping servlet request url request object minus context path , path parameters. url path mapping rules below used in order. the first successful match used no further matches attempted:
- the container try find exact match of path of request path of servlet. successful match selects servlet.
- the container recursively try match longest path-prefix. done stepping down path tree directory @ time, using ’/’ character path separator. longest match determines servlet selected.
- if last segment in url path contains extension (e.g. .jsp), servlet container try match servlet handles requests extension. extension defined part of last segment after last ’.’ character.
- if neither of previous 3 rules result in servlet match, container attempt serve content appropriate resource requested. if "default" servlet defined application, used. many containers provide implicit default servlet serving content.
the container must use case-sensitive string comparisons matching.
you should specification of mappings (given below):
in web application deployment descriptor, following syntax used define mappings:
a string beginning
‘/’character , ending‘/*’suffix used path mapping.a string beginning
‘*.’prefix used extension mapping.the empty string
("")special url pattern maps application's context root, i.e., requests of formhttp://host:port/<contextroot>/. in case path info’/’, servlet path , context path empty string(““).a string containing
’/’character indicates "default" servlet of application. in case servlet path request uri minus context path , path info null.all other strings used exact matches only
let @ examples now. consider following set of mappings:
path pattern servlet /foo/bar/* servlet1 /baz/* servlet2 /catalog servlet3 *.bop servlet4
the following behavior result:
incoming path servlet handling request /foo/bar/index.html servlet1 /foo/bar/index.bop servlet1 /baz servlet2 /baz/index.html servlet2 /catalog servlet3 /catalog/index.html “default” servlet /catalog/racecar.bop servlet4 /index.bop servlet4
note in case of /catalog/index.html , /catalog/racecar.bop, servlet mapped “/catalog” not used because match not exact.
now coming problem :)
/path/test belongs 5th point of specification of mappings. means paths ending in /path/test target servlet1.
however /path/test/* qualifies first point of same specification. means that:
.../path/test handled servlet1 and
.../path/test/abc handled servlet2
which verified me in test application.
Comments
Post a Comment