4

I'm at the end of my rope on this one. I'm try to get a super simple webapp up and I can't seem to get tomcat to not 404 static files.

  • I'm using the gradle tomcat plugin with tomcat version 7.0.39
  • My html file is at hey-world/src/main/webapp/index.html
  • My web.xml looks like this:

    <servlet>
        <servlet-name>Resteasy</servlet-name>
        <servlet-class>
            org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
        </servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>HeyWorldApplication</param-value>
        </init-param>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>/static/*</url-pattern>
    </servlet-mapping>
    
    <servlet-mapping>
        <servlet-name>Resteasy</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    

So I thought this setup would map localhost:8080/hey-world/static/index.html to the file, but it 404's everytime. Is this a problem with some convention in the gradle tomcat plugin?

Chris
  • 555
  • 2
  • 9
  • 28
  • 1
    A url-pattern defines what URLs map to what servlets, it does not define the path to static data files -- and in fact it shouldn't do so. – fge Jun 06 '13 at 09:05
  • @fge Since this won't be production code, I'm less concerned with the performance hits of serving static files from tomcat. Likewise for the security concerns of potentially allowing access to `WEB-INF` (which if I understand correctly has been fixed in this version of tomcat?). My understanding is that routing some `url-pattern` to the default servlet would serve files out of the `webapp` directory. – Chris Jun 06 '13 at 09:11

1 Answers1

5

The URL-patterns used in web.xml/servlet-mapping is often a little simplistic. I think in your case, the /* pattern for Resteasy will work as a catch-all, so that no other mapping will really matter.

For debugging, I suggest you remove the Resteasy-servlet altogether, and see if you can serve static files from a custom URL with your mapping. If that works, re-enable Resteasy, but on a different URL-pattern (eg. /rest/*).

If that works, well, then everything really works fine, it's just that the URL-mapping for /* blocks anything else from working.

The easiest solution would probably be to server static files as per default (no mapping), and serve rest-stuff from another URL.

Alternatively use two web apps. One with context root /static, one with context root /.

Harald K
  • 26,314
  • 7
  • 65
  • 111
  • So commenting out `Resteasy` did start serving the file but at `localhost:8080/hey-world/index.html`, but still 404ed `localhost:8080/hey-world/static/index.html` which makes me think the problem is in the static mapping? – Chris Jun 06 '13 at 09:18
  • Why not just put index.html inside a "static" folder in your webapp? Easy solutions always win. ;-) – Harald K Jun 06 '13 at 09:20
  • Okay now I don't think the servlet mappings are doing anything at all. I switched the static files to not use a mapping and put Resteasy at `/api/*` and now it serves the static files fine but 404s `localhost:8080/hey-world/api/path-to-resteasy-stuff` – Chris Jun 06 '13 at 09:29
  • 1
    Oop. If you put Resteasy behind another url you need to provide a `` [that specifies what that is.](http://stackoverflow.com/questions/4131968/resteasy-path-requiring-a-full-path) Everything is working now. Thanks! – Chris Jun 06 '13 at 09:40