GetServletContext (). getRealPath () in Tomcat 8 returns invalid path

I am trying to run a Java web project under Tomcat 8, which traditionally runs under WebSphere. One servlet makes the following call:

xslFilePath = config.getServletContext().getRealPath(System.getProperty("file.separator") + "xsl"); 

config is an instance of ServletConfig.

xsl is located inside the project and is deployed as C: \ myproject \ build \ web \ xsl. When a servlet tries to reference a file located in xslFilePath, I get an exception that indicates that Tomcat is actually looking for the xsl file in C: \ Program Files \ Apache Software Foundation \ Apache Tomcat 8.0.3 \ bin \ null. Obviously, this is the wrong location and nothing was found.

Unfortunately, I cannot change the code because I do not have access to the source. So I would like to know if this is the expected behavior for Tomcat? Is there any Tomcat configuration that will allow me to guarantee that the path refers to the deployment directory and not to the Tomcat bin directory? Would it be preferable to choose a different servlet container? Any advice would be appreciated.

+5
source share
1 answer

Use getRealPath ("/ xsl").

The parameter for getRealPath () is the "virtual path", which, unfortunately, is a concept used in Java docs, but not actually defined anywhere. This is assumed to be the path to the resource in your web application, and the delimiter in this case is always "/" regardless of platform.

+9
source

Source: https://habr.com/ru/post/1201351/


All Articles