File path to resource in our war/WEB-INF folder?
There's a couple ways of doing this. As long as the WAR file is expanded (a set of files instead of one .war file), you can use this API:
ServletContext context = getContext();
String fullPath = context.getRealPath("/WEB-INF/test/foo.txt");
http://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/ServletContext.html#getRealPath(java.lang.String)
That will get you the full system path to the resource you are looking for. However, that won't work if the Servlet Container never expands the WAR file (like Tomcat). What will work is using the ServletContext's getResource
methods.
ServletContext context = getContext();
URL resourceUrl = context.getResource("/WEB-INF/test/foo.txt");
or alternatively if you just want the input stream:
InputStream resourceContent = context.getResourceAsStream("/WEB-INF/test/foo.txt");
http://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/ServletContext.html#getResource(java.lang.String)
The latter approach will work no matter what Servlet Container you use and where the application is installed. The former approach will only work if the WAR file is unzipped before deployment.
EDIT:
The getContext() method is obviously something you would have to implement. JSP pages make it available as the context
field. In a servlet you get it from your ServletConfig
which is passed into the servlet's init()
method. If you store it at that time, you can get your ServletContext any time you want after that.
Accessing resources located within WEB-INF folder
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.
What is WEB-INF used for in a Java EE web application?
The Servlet 2.4 specification says this about WEB-INF (page 70):
A special directory exists within the application hierarchy named
WEB-INF
. This directory contains all things related to the
application that aren’t in the document root of the application. The
WEB-INF
node is not part of the public document tree of the
application. No file contained in theWEB-INF
directory may be served
directly to a client by the container. However, the contents of the
WEB-INF
directory are visible to servlet code using thegetResource
andgetResourceAsStream
method calls on theServletContext
, and may
be exposed using theRequestDispatcher
calls.
This means that WEB-INF
resources are accessible to the resource loader of your Web-Application and not directly visible for the public.
This is why a lot of projects put their resources like JSP files, JARs/libraries and their own class files or property files or any other sensitive information in the WEB-INF
folder. Otherwise they would be accessible by using a simple static URL (usefull to load CSS or Javascript for instance).
Your JSP files can be anywhere though from a technical perspective. For instance in Spring you can configure them to be in WEB-INF
explicitly:
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" >
</bean>
The WEB-INF/classes
and WEB-INF/lib
folders mentioned in Wikipedia's WAR files article are examples of folders required by the Servlet specification at runtime.
It is important to make the difference between the structure of a project and the structure of the resulting WAR file.
The structure of the project will in some cases partially reflect the structure of the WAR file (for static resources such as JSP files or HTML and JavaScript files, but this is not always the case.
The transition from the project structure into the resulting WAR file is done by a build process.
While you are usually free to design your own build process, nowadays most people will use a standardized approach such as Apache Maven. Among other things Maven defines defaults for which resources in the project structure map to what resources in the resulting artifact (the resulting artifact is the WAR file in this case). In some cases the mapping consists of a plain copy process in other cases the mapping process includes a transformation, such as filtering or compiling and others.
One example: The WEB-INF/classes
folder will later contain all compiled java classes and resources (src/main/java
and src/main/resources
) that need to be loaded by the Classloader to start the application.
Another example: The WEB-INF/lib
folder will later contain all jar files needed by the application. In a maven project the dependencies are managed for you and maven automatically copies the needed jar files to the WEB-INF/lib
folder for you. That explains why you don't have a lib
folder in a maven project.
How can I access a file in WEB-INF/META-INF in a java web app?
Try using this:
URL resource=this.getClass().getClassLoader().getResource("META-INF/test.txt");
as your /META-INF is in your root path you will have to use META-INF/test.txt to access it.
Related Topics
Obtaining a Powerset of a Set in Java
Most Simple Code to Populate Jtable from Resultset
How to Parse a Mathematical Expression Given as a String and Return a Number
Auto Errors Detection in Intellij Idea
Sorting a Collection of Objects
How to Wire One Pane to Another
Display Indeterminate Jprogressbar While Batch File Runs
Look and Feel Is Not Updating in Swing Jtabbedpane
Why Is Spring's Applicationcontext.Getbean Considered Bad
Initial Bytes Incorrect After Java Aes/Cbc Decryption
How to Know If Other Threads Have Finished
How to Load a Jar File at Runtime
Are Static Fields Open for Garbage Collection
Page Scroll Up or Down in Selenium Webdriver (Selenium 2) Using Java
How to Convert Byte Size into a Human-Readable Format in Java