Bug 57556 - getServletContext().getRealPath("/") returns path not ending with /
getServletContext().getRealPath("/") returns path not ending with /
Status: RESOLVED FIXED
Product: Tomcat 8
Classification: Unclassified
Component: Specification APIs
8.0.x-trunk
PC All
: P2 major (vote)
: ----
Assigned To: Tomcat Developers Mailing List
:
Depends on:
Blocks:
  Show dependency tree
 
Reported: 2015-02-10 10:37 UTC by karthic
Modified: 2015-10-30 10:14 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description karthic 2015-02-10 10:37:15 UTC
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.
Comment 1 karthic 2015-02-10 12:13:17 UTC
I mentioned I used till Tomcat6, it is typo, I have been using this code from Tomcat5.5 to Tomcat7
Comment 2 Mark Thomas 2015-02-10 12:23:01 UTC
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.
Comment 3 karthic 2015-02-10 14:06:10 UTC
(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
Comment 4 Mark Thomas 2015-02-10 20:55:42 UTC
Fixed in trunk for 9.0.x and in 8.0.x for 8.0.19 onwards.
Comment 5 Matt 2015-04-27 00:15:53 UTC
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.
Comment 6 Christopher Schultz 2015-04-27 14:29:57 UTC
(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.
Comment 7 Chuck Caldarale 2015-04-27 14:32:17 UTC
(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.
Comment 8 Mark Thomas 2015-04-28 20:41:53 UTC
(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.
Comment 9 Mark Thomas 2015-04-28 21:31:28 UTC
Updated fix in trunk and 8.0.x for 8.0.22 onwards.
Comment 10 James Ching 2015-10-30 08:56:25 UTC
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)
Comment 11 Mark Thomas 2015-10-30 10:14:08 UTC
See bug 56890 and the SPEC bug linked there.