URLs and protocol handlers
We've seen how a directory service implementation lets us abstract away the details of how a context's resources are made available. But that is only part of the story.
In this section, we'll look at how we might plug into Java's standard protocol handler mechanism to make the identification and retrieval of resources easy and intuitive. So please fasten your seatbelts as we enter the exotic world of Java protocol handlers!
Accessing resources
When a servlet container element, such as a servlet or filter, needs to access a static resource, it should avoid using hardcoded paths to that file. Hardcoding paths can hurt the portability of an application across servlet containers or result in failure when the web application is run from within a packed WAR file.
The recommended approach is to use the getResource()
and getResourceAsStream()
methods defined by the javax.servlet.ServletContext
interface. These methods allow access to a packaged resource, independently of...