Absolute Path Relative URL

what i am trying to do is read the content of the html file. When the link is clicked, the content of the html file needs to be read and this content will be used somewhere in my code.

What I have tried:

I tried to convert the relative path to absolute path

String absoluteFilePath = servletContext.getRealPath("/html/en/TestPage.html");

      

and this works fine in weblogic on my windows machine , but when this code is deployed to webloic on a Unix machine, the above code returns null. I dug google and i found

"servletContext.getRealPath returns null if the servlet container is unable to translate the virtual path to the real path for any reason (for example, when content is provided from a .war archive).

Can you suggest a better way to convert a relative path to an absolute one (using the context path). The code should work on both windows and Unix platform or some other way to read the html file, for example instead of reading from an absolute path, if I can read the file from a relative path.

+3


source to share


1 answer


You can only use getRealPath(...)

if war is blown up, but you can always read the file under the root of the servlet context with getResource(...)

or getResourceAsStream(...)

. In your example, this would be:

InputStream is = servletContext.getResourceAsStream("/html/en/TestPage.html");

      



And then you can safely read it ...

+1


source







All Articles