Short answer
To get the result you want, use HttpServletRequest#getServletPath()
method as an argument to getRealPath()
method.
This is the closest to what you want to accomplish (read the note below).
Explanation
The reason you're getting such path (with double blog
) is that you're using the result returned by the getRequestURI() method.
The getRequestURI() method returns the path starting with application context. In your case it will be:
/blog/test-file.html
What happens then, the getRealPath() method appends the string returned by getRequestURI() method to the real/physical path to the folder, where you application resides on the file system, which in your case is:
/usr/share/tomcat7/webapps/blog/
So the resulting path is:
/usr/share/tomcat7/webapps/blog/blog/test-file.html
That is the reason of your double blog
issue.
IMPORTANT NOTE
DISCLAIMER
Maybe the OP is already aware of the information written below, but it is written for the sake of completeness.
The real path you are trying to get does not mean you are getting, well, the real path on your file system. The url-pattern
configured in web.xml (or if you're using Servlet 3.0+ in the related annotation) is actually a logical/virtual path, which may or may not relate to the actual, physical path on a file system, i.e. the patterns (paths) specified does not need to exist physically.
Also quote from the ServletContext.getRealPath(String) documentation (emphasis mine):
Gets the real path corresponding to the given virtual path.