03
Jan
HttpServletRequest getRequestURL() is dumb
Every time I have to wrap a Servlet Request for a filter I’m always reminded of the strangeness of getRequestURL() and its brother getRequestURI().
For the most part getRequestURI() makes sense if you know what it does. I say “know” because its javadoc is rather questionable. It returns only the path and not the query parameters, host, port, and scheme. Ideally it should be called getRequestPath() but I can see why that name might be confused with the context path which is a completely different thing.
Now we get to getRequestURL() which returns a StringBuffer? According to the Javadoc I can edit this StringBuffer and add query parameters. However this is extremely misleading as this will do absolutely nothing to the request. In fact getRequestURL() returns a new StringBuffer everytime. Why the hell not just return a String? Some say its for performance (which its not but I’ll leave that as an exercise for later) and some say for convenience but I say its annoying.
Also one would think getRequestURL() would return the query parameters but it does not which also bothers me slightly.
getPathInfo() is even more confusing. I try to avoid using it and instead use getContextPath() and getRequestURI().
The reason these things are annoying is that every time I go write a servlet filter I forget what does what and end up writing some log statements to see the output of each (since I don’t trust the javadoc).