File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes url-pattern for directory match 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 "url-pattern for directory match" Watch "url-pattern for directory match" New topic
Author

url-pattern for directory match

Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

This question is based on HFSJ-2nd edition --> chapter 11 Web app deployment --> pg 619.

According to the book, if following is the url-pattern in the DD:
<servlet-mapping>
<servlet-name>SomeName</servlet-name>
<url-pattern>/fooStuff/*</url-pattern>
</servlet-mapping>


And the request is: http://locathost:8080/AppName/fooStuff

There would be a match in the above scenario.

However, doesn't the container interpret the fooStuff in the browser window as servlet name itself rather than any servlet under the directory fooStuff. In this case shouldn't the answer be 404 - page not found error?


[Edit: I think I posted this topic in the wrong forum. Could someone please move this to SCWCD certification forum (I cannot do it) - Thanks, Nidhi ]


"A problem well stated is a problem half solved.” - Charles F. Kettering
SCJP 6, OCPJWCD
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1660
    
  25

Hi Nidhi,

However, doesn't the container interpret the fooStuff in the browser window as servlet name itself rather than any servlet under the directory fooStuff. In this case shouldn't the answer be 404 - page not found error?

I am not sure what you are trying to ask here.

The container finds a match on the directory pattern (/fooStuff matches with /fooStuff/*) and looks up which Servlet is mapped to that pattern. In this case the request will be directed to the Servlet with name SomeName (I am missing the <servlet> tag in your question)

Regards,
Frits
Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

Fritz,

Thanks for the answer. I guess I was needlessly complicating it. Somehow I thought if the user enters www.example.com/fooStuff, it will NOT match a pattern of:
<url-pattern>/fooStuff/* </url-pattern>

(Since one is resource name & the second is directory), but I guess the container doesn't see it that way.

Please correct me if I am wrong.
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1660
    
  25

(Since one is resource name & the second is directory), but I guess the container doesn't see it that way.


Well, there are two steps:
  • match to the web-application
  • match to the servlet


  • this is how the spec describes the match to the web-application:
    Upon receipt of a client request, the Web container determines the Web application to which to forward it. The Web application selected must have the the longest
    context path that matches the start of the request URL. The matched part of the URL is the context path when mapping to servlets.

    match to the servlet is described like this:
    The path used for mapping to a servlet is the request URL from the request object minus the context path and the path parameters.

    have a look at the picture:

    There must be a match on the contextPath, so in the request URL (http://localhost:8080/AppName/fooStuff ) there will be a match on http://localhost:8080/AppName, where AppName is the (context-root of the) web-application on your server.
    The remaining part ot the request URL (/foostuff) will be used to match against the url-patterns in your web.xml

    Be careful: /foostuff doesn't have to be a valid directory in your web-app! It is a virtual mapping.

    Does this clear your doubts?

    Regards,
    Frits
    Nidhi Sar
    Ranch Hand

    Joined: Oct 19, 2009
    Posts: 252

    Great answer. Thanks!
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: url-pattern for directory match