I setup COMET functionality in my web-app (with the help of such Servlet 3.0 features as AsyncContext and startAsync) to allow for things such as real-time notifications. After adding a filter in front of the sole servlet, I realized that this functionality no longer works because responses are committed automatically after a forward.
Requests that setup COMET functionality require that the responses NOT be committed after the request is handled. This means that I can neither use a forward or redirect to pass processing to my servlet. Thus, i’m stuck using doFilter(), which to my knowledge, does not automatically commit responses after execution.
I was thinking I could wrap requests in a custom HTTPServletRequestWrapper which overrides all the methods dealing with URLs, like so:
public class ActionServletRequestWrapper extends HttpServletRequestWrapper
{
public ActionServletRequestWrapper(HttpServletRequest request)
{
super(request);
}
@Override
public String getRequestURI()
{
String originalRequestURI = super.getRequestURI();
int lastSlashIndex = originalRequestURI.lastIndexOf("/");
return originalRequestURI.substring(0, lastSlashIndex) + "/ActionServlet";
}
@Override
public StringBuffer getRequestURL()
{
String originalRequestURL = super.getRequestURI();
int lastSlashIndex = originalRequestURL.lastIndexOf("/");
return new StringBuffer(originalRequestURL.substring(0, lastSlashIndex) + "/ActionServlet");
}
@Override
public String getServletPath()
{
return "/ActionServlet";
}
}
…but passing this in to doFilter() does not seem to pass execution on to ActionServlet (though through breakpoints I found that the overridden getServletPath() is called at some point). Are there any additional methods I need to overload, or are requests unable to be re-directed in this way?
Considering the fact that no one has answered the question yet, and all my attempts to filter a request in this way have failed, I’m going to go ahead and assume the answer to this question is:
No, you cannot filter a request to a different destination by wrapping it and overloading all of the URL-related methods. The code which determines the destinations of requests apparently does not rely on those methods.