Inspired by How to access static resources when mapping a global front controller servlet on /*
I came up with this solution:
in /war/WEB-INF/web.xml
:
<filter>
<filter-name>MainFilter</filter-name>
<filter-class>com.example.mywebsite.MainFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MainFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>MainServlet</servlet-name>
<servlet-class>com.example.mywebsite.MainServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MainServlet</servlet-name>
<url-pattern>/MainServlet/*</url-pattern>
</servlet-mapping>
in MainFilter.java
:
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
String path = req.getRequestURI();
String topfolder = path.substring(1);
if (topfolder.contains("/")) {
topfolder = topfolder.substring(0, topfolder.indexOf("/"));
}
if (topfolder.startsWith("_")) {
chain.doFilter(request, response);
} else if (topfolder.endsWith(":")) {
request.getRequestDispatcher(path.replaceFirst(":", "")).forward(request, response);
} else {
request.getRequestDispatcher("/MainServlet" + path).forward(request, response);
}
}
Now you can put all your static content into subfolders in your WAR. Then if you want to link to /war/css/style.css
from your HTML, you simply refer to it as "/css:/style.css"
.. Or you can name your folders/files with a _
at the beginning and refer to them like normal..
(Also the _
rule makes sure that Google App Engine developers can access /_ah/admin
)