Sc.getRealPath (.) And File.seperator on SDK8 and Tomcat 8

I have an application that works fine on both Windows and Linux using Tomcat 6 and 7 and SDK 6 and 7. I am setting up SDK 8 and Tomcat 8, and when the application launches it, it now chokes:

public static final String THEME_DIRECTORY = File.separator + "WEB-INF"
                                                + File.separator + "theme"
String realPath = servletContext.getRealPath(requiredPath);

      

as realPath

returns null

. It turns out that the following is true:

  • requiredPath

    is '\ WEB-INF \ theme';
  • File.separator

    is '\';
  • If the parameter is requiredPath

    set to "/ WEB-INF / theme" then I will return it realPath

    as expected.

The behavior seems to have changed and I can no longer use File.separator

.

Has anyone else encountered this?

+3


source to share


2 answers


Yes, I ran into this too. I suspect this is a stricter implementation of the Servlet specification in Tomcat 8 as opposed to 7. The javadocs here seem to indicate that the path parameter is assumed to be a URL fragment, which means it should use forward slashes, not backslash, which is what you get when you use File.separator on Windows.



+2


source


It depends on your Apache Tomcat 8 version.



I figured out the backslash in the getRealPath () method works on the latest Tomcat, but it doesn't work on versions prior to 8.0.29.

0


source







All Articles