0

I have a JSP/Servlet based web application that uses Maven and has not been implemented using Spring MVC. I'm trying to deploy that to an embedded tomcat container using webapp-runner.

The problem is that the css/js and other asset references from the JSP do not load those resources. When I start the application using java -jar target/dependency/webapp-runner.jar target/myapp.war, I'm able to access the page using http://localhost:8080/index.jsp. However, the resources are not present at http://localhost:8080/assets/css/sample-css.css (example reference).

Notice that there is no "webcontext" at all which I'm thinking is the way embedded tomcat works. Any ideas on what may be wrong ?

This is my directory structure:

-- src
    --- main
        -- java
            --- <servlet related classes in package folder>
        -- webapp
            -- WEB-INF
                -- web.xml
            -- assets
                -- js
                -- css
                -- fonts
                -- img
            -- index.jsp

When I package this application, the WAR, which is present in <project-dir>/target looks like this.

            -- META-INF
            -- WEB-INF
                -- web.xml
            -- assets
                -- js
                -- css
                -- fonts
                -- img
            -- index.jsp

This is the relevant part of pom.xml:

  ...
    <plugins>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <warSourceDirectory>${basedir}/src/main/webapp</warSourceDirectory>
                <warName>${project.artifactId}</warName>
                <failOnMissingWebXml>false</failOnMissingWebXml>                    
            </configuration>
        </plugin>
      ...

This is how I access resources in the JSP page. I've tried a few styles but nothing seems to work:

 <link type="text/css" href="${pageContext.request.contextPath}/assets/css/bootstrap.min.css" rel="stylesheet">
 <!-- Another way -->
 <link type="text/css" href="assets/css/bootstrap.min.css" rel="stylesheet">

I've referred How to link a web resource file from /main/resources in JSP? and the resources it points to.

Community
  • 1
  • 1
markgarg
  • 152
  • 7

1 Answers1

0

For anyone who's faced same problem before, he's how I've solved it (temporarily): I've moved over from Servlet 3.1 to 2.5 and that solved it. I can't understand why, but it now works.

markgarg
  • 152
  • 7