Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getServletContext().getRealPath() in Tomcat 8 returns wrong path

I am attempting to run a Java web project under Tomcat 8 that 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.

The xsl is inside the project and deployed as C:\myproject\build\web\xsl. When the servlet attempts to reference a file located in xslFilePath, I get an exception which 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 is found.

Unfortunately I can't change the code because I don't 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 let me ensure the path is referenced to the deployment directory and not to the Tomcat bin directory? Would choosing some other servlet container be preferable? Any advice would be appreciated.

like image 879
MichaelB Avatar asked Aug 28 '14 18:08

MichaelB


People also ask

Why does getrealpath() return NULL in servlet?

"This method returns null if the servlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war archive)." argument to getRealPath () through a .war, getRealPath () will return null. 1. You unpack the .war file manually with jar xf and delete it afterwards. 2.

How many servletcontext getrealpath examples are there?

Java ServletContext.getRealPath - 30 examples found. These are the top rated real world Java examples of javax.servlet.ServletContext.getRealPath extracted from open source projects.

How to change the argument to getrealpath() through a war?

argument to getRealPath () through a .war, getRealPath () will return null. 1. You unpack the .war file manually with jar xf and delete it afterwards. 2. In the Tomcat's config file `server.xml' you change the Host directive's argument from unpackWARs="false" to unpackWARs="true"


1 Answers

Use getRealPath("/xsl").

The parameter for getRealPath() is a 'virtual path' which - unfortunately - is a concept used in the Java docs but not actually defined anywhere. It is assumed to be a path to a resource in your web application and the separator in that case is always '/' regardless of platform.

like image 90
Mark Thomas Avatar answered Nov 03 '22 04:11

Mark Thomas