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, I'am new to Servlet and studying SCWCD by reading Deshmukh/Malavia 's exam study kit. In section 5.2.4 of the book, Mapping of ServletURL to Servlet Path and Path Info are discribed. Could anyone tell me what is the use of Servlet Path and Path Info as it seems the use of these "paths" are not explained. Why take the trouble to extract them from the servlet URL ?
CS: Declaratively It's easy to maintain/establish the rules of URL mapping to the servlets in a DD. Programmatically one could determine from which URL the request had come,if one comes to know about the pathInfo. Any more ideas ?
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 ]
Joined: Nov 05, 2002
Thanks Guys, I get it now. So these "paths" are nothing to do with the directory path location of servlet class file location.