Remember that in the web.xml file, servlet definition and servlet mapping are distinct. This means that a single servlet can be mapped to more than one URL. When you do this, you probably want the servlet to behave differently depending on the URL used. That is what you need getServletPath() for.
Example: you have a servlet "documentServlet" which serves up documents from a library stored in a database. It is mapped to both "/documents/public" and "/documents/secret" (obviously the "/documents/secret" path is restricted using web.xml security settings). Depending on which path is used, the servlet needs to use a different database to read documents from. You might have a little configuration file for the servlet allowing you to associate databases with any number of paths.
Wildcard servlet mappings are a great way to set up virtual filesystems. The servlet takes the extra path information and interprets it in some way; from the outside world it looks like an entire filesystem lives underneath the servlet. Of course here is where getPathInfo() kicks in, because that's how you find out about the wildcard bit of the mapping.
Let's continue our previous example. Say the documentServlet is mapped to "/documents/public/*". You could request the document "/documents/public/employment/policy.doc". The servlet would see a servlet path of "/documents/public", so it would know to access the public documents database. It would also see path info "/employment/policy.doc", and turn this into a primary key for the database query so that the right document is retrieved.
An end user would know nothing about this. (S)he would simply know that documents are living in a filesystem under "/documents". Better even, if you would implement the WebDAV protocol in your servlet, this filesystem would look just like any other Windows folder, with no hint that this stuff is actually living in a database. But WebDAV would take us outside the realm of the SCWCD exam, so I'll stop right here
- Peter
[ November 07, 2002: Message edited by: Peter den Haan ]