This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi Arpana, I think A, B are correct. I am not sure about D, because I am not clear about what is default servlet. You know A is correct. B is correct because of third mapping *************** <servlet-mapping> <servlet-name>DoServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> ***************** Choice D would be correct if it says /dothat request is handled by the DoThatServlet servlet I am not sure if DoThatServlet is the default servlet.
ortimus, you are right. Answer B. is incorrect. Wow, it is a trick question. I will watch out for such 'traps' in the exam. Actually it is not the longest path that matches the pattern but first pattern in the servlet path (which is /do/this/ and NOT *.do) that finds a match in the servlet-mappings.
Actually, I over looked at the fact that /do/this/whatto.do matches both mappings (/do/this/* and *.do)
i think, ortimus is correct. it is the longest path in the servlet-mappings that matches the pattern and NOT the first pattern in the servlet path (as said by Manisha).
the order is not important. ( or is it ? )
To know recursion, you must first know recursion...
Joined: Mar 05, 2001
Sorry for the confusion. What I was trying to say was, it is the first strip of URI string that has a matching url-pattern that counts and not just the longest. Container takes the whole URI and starts stripping from the end of URI to the next "/" and tries to find a match.
For examples, if I have following mappings <servlet-mapping> <servlet-name>DoxyzServlet</servlet-name><url-pattern>/do/this/ab/cd/ef/jk/*</url-pattern> </servlet-mapping>
and I use <host><context path>/12/do/this/whatto.do or <host><context path>/12/do/this/ab/cd/ef/jk/whatto.do
Do12Servlet will get executed, because /12/* is the matching mapping and not /do/this/ab/cd/ef/jk/*
Joined: Dec 13, 2003
If you have another mapping defined after the existing mappings: <servlet-mapping> <servlet-name>DoThisServlet</servlet-name> <url-pattern>/12/do/this/*</url-pattern> </servlet-mapping>
Then <host><context path>/12/do/this/whatto.do or <host><context path>/12/do/this/ab/cd/ef/jk/whatto.do
will both map to DoThisServlet and not Do12Servlet even though the Do12Servlet mapping appears before DoThisServlet and does match the pattern. DoThisServlet is a longer match.
Thats what I meant by longest matching pattern. Its the longest match found while stepping down the path tree, a directory at a time using '/' as a path separator. Ofcourse, if an exact match was found for either pattern, that would take precedence and no further match will be attempted. Check out the servlet specs SRV.11.1 - "Use of URL Paths".
I actually tried it out using Tomcat, confirmed results and posted that reply. I created all the DoServlet, DoThisServlet, DoThatServlet, DoxyzServlet and Do12Servlet. I added all 5 mappings as discussed. I got exact results as I have mentioned in my previous reply. I don't have anything else to say.
BTW, I passed my SCWCD(310-080) exam last saturday with 81%.
Thanks for the discussion. I learnt some tips about servlet mapping including default servlet.