Usman Mutawakil
Usman Mutawakil

Reputation: 5259

Browser is unable to retrieve JS files from WEB-INF folder

I have a folder, "js", inside my WEB-INF folder and it holds javascript files. I can't get the server to retrieve them using the url localhost:8084/appname/js/file.js but everything else defined in my web.xml file works fine.

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app>

   <servlet>
        <servlet-name>MeasurementNodesController</servlet-name>
        <servlet-class>com.mycompany.MeasurementNodesController</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>MeasurementNodesController</servlet-name>
        <url-pattern>/MeasurementNodes</url-pattern>
    </servlet-mapping>

    <resource-ref>
        <res-ref-name>jdbc/datasource</res-ref-name>
        <res-ref-type>javax.sql.DataSource</res-ref-type>
    </resource-ref>

    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

Upvotes: 1

Views: 2585

Answers (3)

Du-Lacoste
Du-Lacoste

Reputation: 12777

This was answered already but I'm providing visual detail...Files in WEB-INF are inaccessible to the outside world and only can be reached by your application itself.

You need to put your public static assets at the top level of your war file such as,

js/file.js

In order to retrieve you can do the following:

<script src="js/file.js"></script>

enter image description here

Upvotes: 0

navyad
navyad

Reputation: 3860

Deployment structure of tomcat

you will end up with the following contents in your application's "document root" directory:

*.html, *.jsp, etc. - The HTML and JSP pages, along with other files that must be visible to the client browser (such as JavaScript, stylesheet files and images) for your application. it is generally much simpler to maintain only a single directory for these files.

/WEB-INF/web.xml - This is an XML file describing the servlets and other components that make up your application, along with any initialization parameters and container-managed security constraints.

/WEB-INF/classes/ - This directory contains any Java class files (and associated resources) required for your application.

/WEB-INF/lib/ - This directory contains JAR files that contain Java class files (and associated resources) required for your application, such as third party class libraries or JDBC drivers.

Upvotes: 0

Will Hartung
Will Hartung

Reputation: 118714

Anything in the WEB-INF folder is unavailable to the outside world. WEB-INF is only available internally for Forwards and such.

You'll need to move the JS out of WEB-INF to be able to use it.

Upvotes: 5

Related Questions