I have been using this "getServletContext().getRealPath("/")" till Tomcat6 and i got the return string path ending with slash. For Example getServletContext().getRealPath("/") will return E:/TomcatHome/webapps/CRM/ But when I tried with Tomcat 8.0.14 I got path without slash For Example getServletContext().getRealPath("/") will return E:/TomcatHome/webapps/CRM Ending slash missing Is there anything changed in Tomcat 8 and is there any configuration I can make to get the result that I got in previous version of Tomcat, because if I have to change in code, I have to change in lot of places in all the webapps I created.
I mentioned I used till Tomcat6, it is typo, I have been using this code from Tomcat5.5 to Tomcat7
This will be the new resources implementation introduced in 8.0.x. I don't see any reason not to restore the trailing separator for a directory for backwards compatibility but I say that not having looked at the code.
(In reply to Mark Thomas from comment #2) > This will be the new resources implementation introduced in 8.0.x. > > I don't see any reason not to restore the trailing separator for a directory > for backwards compatibility but I say that not having looked at the code. Mark, in that code I am just getting the path but that will be sent to another method where the file name is added dynamically or event based. Is there anyway I can make this work as it was previous without changing code. If there is no workaround, then I should downgrade to Tomcat 7 . Thanks Karthic
Fixed in trunk for 9.0.x and in 8.0.x for 8.0.19 onwards.
Tomcat 6 only returned a path with an ending slash when asking for "/", any other directory path does not end in a slash. So this is actually breaking backwards compatibility. In my opinion, what makes the most sense would be for it to return an ending slash only if the path requested ends in a slash, but being consistent with previous behavior is a good idea too.
(In reply to Matt from comment #5) > Tomcat 6 only returned a path with an ending slash when asking for "/", any > other directory path does not end in a slash. So this is actually breaking > backwards compatibility. In my opinion, what makes the most sense would be > for it to return an ending slash only if the path requested ends in a slash, > but being consistent with previous behavior is a good idea too. I tend to agree that slashes shouldn't be added to anything. But my position is that this method should unconditionally return null.
(In reply to Christopher Schultz from comment #6) > But my position is that this method should unconditionally return null. +1 on that. Anyone using this method for anything other than debugging is fooling themselves.
(In reply to Chuck Caldarale from comment #7) > (In reply to Christopher Schultz from comment #6) > > But my position is that this method should unconditionally return null. > > +1 on that. Anyone using this method for anything other than debugging is > fooling themselves. Indeed but this request looks reasonable. I'll take a look.
Updated fix in trunk and 8.0.x for 8.0.22 onwards.
Is it also by design to change the behavior of ServetContext.getRealPath("") (now always appended with trailing slash) and discontinue support for ServetContext.getRealPath(".") and its variations? In Tomcat 7.0.41, getRealPath("") returns webapp root without trailing slash, and naturally getRealPath("/") comes with a trailing slash. getRealPath(".") returns webapp root appended with ".", same result as getRealPath("/."). E.g. in Windows ServletContext.getRealPath("") returns C:\\path\\to\\webapp\\root ServletContext.getRealPath("/") returns C:\\path\\to\\webapp\\root\\ ServletContext.getRealPath(".") returns C:\\path\\to\\webapp\\root\\. ServletContext.getRealPath("./") returns C:\\path\\to\\webapp\\root\\. ServletContext.getRealPath("/.") returns C:\\path\\to\\webapp\\root\\. ServletContext.getRealPath("./foo") returns C:\\path\\to\\webapp\\root\\.\\foo In Tomcat 8.0.28, ServletContext.getRealPath("") always contains trailing slash and getRealPath(".") returns null and any path starting with it. Also counter-intuitively, getRealPath("/.") returns webapp root without the trailing dot nor slash. E.g. ServletContext.getRealPath("") returns C:\\path\\to\\webapp\\root\\ ServletContext.getRealPath("/") returns C:\\path\\to\\webapp\\root\\ ServletContext.getRealPath(".") returns null ServletContext.getRealPath("./") returns null ServletContext.getRealPath("./foo") returns null ServletContext.getRealPath("/.") returns C:\\path\\to\\webapp\\root Is it possible to restore previous behavior as in Tomcat 7? (This is inclusive of the reported behavior above)
See bug 56890 and the SPEC bug linked there.