1

I have a Tomcat server, serving two .war files. The first one is mapped to context /api, the second one to root /. The latter contains a single-page AngularJS app. It has the following web.xml config:

<servlet>
    <servlet-name>webapp</servlet-name>
    <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
    <init-param>
        <param-name>debug</param-name>
        <param-value>0</param-value>
    </init-param>
    <init-param>
        <param-name>listings</param-name>
        <param-value>false</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>webapp</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<error-page>
    <error-code>404</error-code>
    <location>/</location>
</error-page>
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

When I go to https://my.url/, the index page of the single page app is properly served. So far, so good.

The problem is, when I deeplink to a page in my single page app, for example https://my.url/some/resource, Tomcat will give a 404. Because of the error-page config, it will still return the index page, but still with status 404. So, it kind of works, but not nicely.

Can I get Tomcat to return the index page with a proper 200 status code for all deep links? Of course, calls to /api should still resolve to the other deployed .war. I want to avoid duplicating the AngularJS url definitions in Tomcat, so it should just return the index page for any request that doesn't start with /api/.

Jorn
  • 20,612
  • 18
  • 79
  • 126

1 Answers1

-2

For deeplinks you need to use wildcards on your webapp url pattern. Something like:

<url-pattern>/*</url-pattern>

Not sure would it not catch the /api/ calls then. Some more advanced url pattern might be required.

topr
  • 4,482
  • 3
  • 28
  • 35
  • I tried that, but it doesn't work. It'll catch the non-`/api` URLs, but won't actually serve the index page for them. Also, [this question](https://stackoverflow.com/q/4140448/8681) rightfully warns against mapping a servlet on `/*`. – Jorn Jun 15 '17 at 14:16
  • 1
    What's the point of serving static contect (angular webapp) with Tomcat in the first place? Just use Apache and use _mod rewrite_. You can use the same Apache instance to proxy tomcat with your REST API to have it all under the same domain and port. – topr Jun 15 '17 at 15:28