How to Read Files Contained in a Web Application / WAR
If your servlet requires any additional files, put those data files into /WEB-INF/ (more). ServletContext offers three ways of reading them:
| Method | Returns | Required? |
|---|---|---|
| getResourceAsStream | InputStream to read the file | Yes |
| getResource | URL of the file | Yes |
| getRealPath | Path to file (String) | No |
Usually getResourceAsStream is the most useful of the three. The following example uses it to read a Properties file:
Properties config = new Properties();
config.load(getServletContext().getResourceAsStream("/WEB-INF/config.properties"));

