can you deploy a servlet to '/' (serving every request)

50 views
Skip to first unread message

Stewart Sims

unread,
May 16, 2016, 3:59:32 PM5/16/16
to Google App Engine
Is it possible to map a Java servlet to '/' like you can with any Servlet container (as clarified in the Servlet 3 spec) to serve every request, other than those specifically mapped to other URL patterns?

Setting any servlet mapping path (URL pattern) to '/' seems to break deployed apps, and using '/*' doesn't seem to capture requests beyond that level in the hierarchy (e.g. only works for /someResource and not /images/someResource or /scripts/someResource).

Nick (Cloud Platform Support)

unread,
May 17, 2016, 3:50:48 PM5/17/16
to Google App Engine
Hey Stewart,

This is a question which would be better asked on Stack Overflow, since it's not specifically about App Engine. I'll be happy to provide an answer, but be sure to check the context of the forum in future, since Stack Overflow doubtless has a lot more users and actually has answers on this exact question already. You'll be more likely to find an answer when considering the context of the forum. So:

My testing doesn't show that /* is behaving this way when deployed to production. It matches any number of nested slashes without problem. Although then again, I'm running a standard App Engine java runtime, which runs on Servlet 2.5 spec. However, regardless, there's a reason you don't want to use this pattern anyways, since it causes a StackOverflowError when you want to use RequestDispatcher#forward(), which is a reasonably useful method. As explained in this StackOverflow answer, you should use a ServletFilter on /* which can perform routing as needed.

Cheers,

Nick
Cloud Platform Community Support

Nick

unread,
May 17, 2016, 6:13:01 PM5/17/16
to Google App Engine
This works, but the matching patterns for servlets are unintuitive, so it can be very hard to get this to work as you want. I can't say about it matching the servlet 3 spec, I'd almost guarantee not.

Is there something specific you're trying to achieve?

The usual symptom when attempting to do this is to break all the servlet internal dispatching, such as the default servlet and jsp rendering.

Using the thundr framework, this is the standard web.xml:

<servlet>
<servlet-name>thundr</servlet-name>
<servlet-class>com.threewks.thundr.ThundrServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>thundr</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<!-- Objectify filter - required for Objectify to work -->
<filter>
<filter-name>ObjectifyFilter</filter-name>
<filter-class>com.googlecode.objectify.ObjectifyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ObjectifyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

Reply all
Reply to author
Forward
0 new messages