I’m working on a servlet that effectively routes to other servlets, performing some nifty helper functions along the way. My web.xml is configured to direct all traffic through this class, and the class routes the requests based on pathInfo and regex logic. My code looks like this:
public class API1 extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String pathInfo = req.getPathInfo();
if (pathInfo.matches("/.*/.*/.*/?")) {
_fooBarBazHandler.get(req, resp);
} else if (pathInfo.matches("/.*/.*/?")) {
_fooBarHandler.get(req, resp);
} else if (pathInfo.matches("/.*/?")) {
_fooHandler.get(req, resp);
} else {
resp.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
}
...
}
Now, the idea is that if I hit http://host/FooService/foo/bar/baz that it would redirect to the _fooBarBazHandler, http://host/FooService/foo/bar would hit the _fooBarHandler, and http://host/FooService/foo would hit the _fooHandler
For some reason, however, http://host/FooService/foo is apparently matching on _fooBarHandler. To be absolutely clear, using the debugger, I’ve ascertained that the value of pathInfo is “/foo”.
My regex isn’t too strong. Any ideas?
the regexp
/.*/.*/?will not match"/foo". your bug is elsewhere (sorry!).see http://fiddle.re/0g28 (click on blue “Java”)