I’m setting up a web app on GlassFish and I’m wondering if there is a way to configure servlet mapping from the URL root to only apply in cases where a file or directory doesn’t exist at the specified URL.
Currently, this will route everything through my Spring dispatcher:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>
/jsp/index.jsp
</welcome-file>
</welcome-file-list>
</web-app>
Is it somehow possible to preserve the above behavior unless a file or directory exists at a given path? Am I trying to solve this problem in the wrong place?
UPDATE: What about URL rewriting? Does GlassFish provide any degree of flexibility like Apache does with mod_rewrite? That way I could intercept things before it even reaches the servlet mapping stage.
Hacky, but doable: map it as
<error-page>on an<error-code>of404.You might want to change the status code in the servlet whenever you want to return a “valid” response.
An alternative is to let the Spring servlet listen on a more specific
url-pattern. E.g./pages/*. You could then if necessary bring aFilterin front (on/*) which does roughly the following:so that you don’t see/need
/pagesin the URL.Update: as per your new question:
The JSP/Servlet equivalent of Apache HTTPD’s
mod_rewriteaddon is the Tuckey’sUrlRewriteFilter.