Accessing resources located within WEB-INF folder

I have been working on a simple Java web application which contains JSP files. I have been attempting to access resource files such as, css file and images which resides within the WEB-INF/classes directory through a JSP file which is located in the context root in the resultant WAR file.

The project structure was created based on the recommended structure under Maven War Plugin usages. I went through several solutions provided for similar issues within this forum but none of them worked for this new structure.

How do we access a resource located in WEB-INF/classes directory from a JSP file located in the context-root for example?
example WAR file with the mentioned structure


If you want to access these resources through HTTP, they must be elsewhere – they can’t be in WEB-INF. That question would be a duplicate as it has been answered elsewhere, multiple times (just look up the related questions links for this question). A Java web application will never make any resources under WEB-INF accessible through HTTP.

However, as your question is about the “recommended” standard file structure, let me tell you where the documentation you link is misleading: Any resources in src/main/resources will end up on the classpath, e.g. in WEB-INF/classes. It will not end up on the directory where the webapplication makes it available to the world. However, the documentation uses an image, which suggests that the image can be used within the webapplication. It can’t. You can access it through the classloader, in your case for example as: SomeClassFromYourWebapp.class.getResourceAsStream(). This is more typically done with .properties files, .xml or similar ones that get processed at runtime than with images. Truly misleading documentation I’d say (but it’s correct, just misleading)

Technically this would answer your question for “how do I access the resource from a JSP?”, because you can have this code in a JSP. I believe that your question is rather asking to address the resources through HTTP.

And the answer to that is: Move the resources that you need available through HTTP to src/main/webapp and keep them out of WEB-INF. Otherwise they’ll only be on the classpath.

Leave a Reply

Your email address will not be published. Required fields are marked *