Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet Mapping

 
Arpana Rai
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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

Rgds,
Arpana
 
Roy Huang
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
agree with U
 
Narsimha Manekar
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Narsimha Manekar
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Congratulations on passing another exam

- orimuS
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic