wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Servlet Mapping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Servlet Mapping" Watch "Servlet Mapping" New topic
Author

Servlet Mapping

Arpana Rai
Ranch Hand

Joined: Nov 12, 2002
Posts: 93


Correct answers giben r A and D, but i think only A is correct. Any comments?

Rgds,
Arpana


SCJP1.4(91%)
Roy Huang
Ranch Hand

Joined: Nov 21, 2001
Posts: 104
agree with U


SCJP 1.4/SCBCD/SCWCD<br />MCSE<br />IBM professional XML Developer<br />SAP certified Development Consultant (NetWeaver portal and Web JavaApplication)
Narsimha Manekar
Ranch Hand

Joined: Mar 05, 2001
Posts: 35
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.

-rao
Sumitro Palit
Ranch Hand

Joined: Dec 13, 2003
Posts: 37
Narsimha,

B. /do/this/whatto.do request is handled by the DoServlet
is not correct.

<servlet-mapping>
<servlet-name>DoThisServlet</servlet-name>
<url-pattern>/do/this/*</url-pattern>
</servlet-mapping>


Its the longest path that matches the pattern =>
/do/this/whatto.do will map to DoThisServlet

-ortimuS
[ May 18, 2004: Message edited by: ortimus tilap ]
Narsimha Manekar
Ranch Hand

Joined: Mar 05, 2001
Posts: 35
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)

thank you
-Rao
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi

I start my prepartion to SCWCD this week. I'll go to take the 1.4 version

The text of the question must be wrong. The only correct answer for me is A.

For D the container tries to match the request URI to a servlet mapping. (/dothat) and it must find it in the servlet path DoThatServlet.

really ?
Gaurav Sahni
Ranch Hand

Joined: Sep 03, 2003
Posts: 56
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 ? )

Gaurav.


To know recursion, you must first know recursion...
Narsimha Manekar
Ranch Hand

Joined: Mar 05, 2001
Posts: 35
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>

<servlet-mapping>
<servlet-name>Do12Servlet</servlet-name><url-pattern>/12/*</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/*


-rao
Sumitro Palit
Ranch Hand

Joined: Dec 13, 2003
Posts: 37
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".

Hope this clears things up.

- ortimuS
[ May 23, 2004: Message edited by: ortimus tilap ]
Narsimha Manekar
Ranch Hand

Joined: Mar 05, 2001
Posts: 35
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.

-rao
Sumitro Palit
Ranch Hand

Joined: Dec 13, 2003
Posts: 37
Congratulations on passing another exam

- orimuS
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet Mapping