32

I have a directory containing a number of static file (*.png, *.css, etc).
I thought (mistakenly perhaps) that just creating a directory in my application's WEB-INF file would suffice and I would be able to access the files by just referring to them by name:
Ex:

   <link rel="stylesheet" href="/static/styles.css" type="text/css">

My directory structure is as follows:

+WEB-INF
   |
   +---static
       |
       +--styles.css
       +--header.png

My web.xml is as follows

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>myapp</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:com/example/myapp/spring/applicationContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <listener>
        <listener-class>
            com.example.myapp.ContextListener
        </listener-class>
    </listener>

    <!--  
        There are three means to configure Wickets configuration mode and they are
        tested in the order given. 
        1) A system property: -Dwicket.configuration
        2) servlet specific <init-param>
        3) context specific <context-param>
        The value might be either "development" (reloading when templates change)
        or "deployment". If no configuration is found, "development" is the default.
    -->

    <filter>
        <filter-name>wicket.myapp</filter-name>
        <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
        <init-param>
            <param-name>applicationFactoryClassName</param-name>
            <param-value>org.apache.wicket.spring.SpringWebApplicationFactory</param-value>
        </init-param>
    </filter>
    <filter>
        <filter-name>wicket.session</filter-name>
        <filter-class>org.apache.wicket.protocol.http.servlet.WicketSessionFilter</filter-class>
        <init-param>
            <param-name>filterName</param-name>
            <param-value>wicket.myapp</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>wicket.myapp</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>wicket.session</filter-name>
        <url-pattern>/servlet/*</url-pattern>
    </filter-mapping>
</web-app>

But....
This doesn't work I just get a 404 - file not found when attempting to access the resources contained in the "static" directory. Is there something I'm missing here?

3 Answers3

60

Don't put them into WEB-INF, that folder is for the stuff that you do not want to have directly served.

Put it next to WEB-INF

+- WEB-INF
+- static
Thilo
  • 257,207
  • 101
  • 511
  • 656
  • 1
    What do you mean by open? If you really need to access it as a file, you can ask your servlet container for the real file system path. – Thilo May 09 '17 at 06:06
  • @PhilipRego Even if you can (not sure if all servlet containers will allow it), you really should not be writing inside of your webapp directory. The whole point of a WAR is that you have a stateless deployment unit that you can just replace with a later version. Much better to write any data to the filesystem outside of your webapp root (or even shared storage or a database). – Thilo May 10 '17 at 23:53
  • Include files in the WAR at build/deploy-time: Of course. That is what a WAR is for. But you should not be writing to files at runtime. – Thilo May 11 '17 at 14:20
  • 1
    It is very common to have a consistent location like `/var/lib/myapplication/data`. Of course, these days, where you tend to need to scala up across multiple machines and be fault-tolerated about it, dedicated storage systems are taking over from writing to files directly. – Thilo May 11 '17 at 23:26
  • @PhilipRego: The `static` folder next to `WEB-INF` is still within the web application content root. It should appear within the normal URL space that has been assigned to the application (for example `https://myserver/myapp/static` ) – Thilo Oct 12 '17 at 08:33
5

The files should not be inside WEB-INF. They must be placed directly inside your web-applications directory or WAR file.

Look at my answer here for including the context path before your static resources.

Community
  • 1
  • 1
Chandra Sekar
  • 10,683
  • 3
  • 39
  • 54
3

WEB-INF is a protected directory. They must be placed in or in a subdirectory in the top level web app directory.

Robby Pond
  • 73,164
  • 16
  • 126
  • 119