Getting the path of WEB-INF folder from a Java Servlet

You may need to get the path of the WEB-INF folder from your Java Servlet for reading custom configuration files or writing temporary files or doing other related work. In order to get the path of the WEB-INF folder (or its sub folders) from your Java Servlet service methods, you can do this by using the getRealPath() API in the ServletContext interface as follows:

String path = getServletContext().getRealPath("/WEB-INF");

If we have for example a folder called xyz under the WEB-INF folder, we can get its path as follows:

String path = getServletContext().getRealPath("/WEB-INF/xyz");
This entry was posted in Java Web and tagged , , , , by Hazem Saleh. Bookmark the permalink.

About Hazem Saleh

Hazem Saleh has more than eleven years of experience in Cloud, Mobile and Open Source technologies. He worked as a software engineer, technical leader, application architect, and technical consultant for many clients around the world. He is an Apache PMC (Project Management Committee) member and a person who spent many years of his life writing open source software. Beside being the author of the "JavaScript Unit Testing" book, "JavaScript Mobile Application Development" book, "Pro JSF and HTML5" book and the co-author of the "Definitive guide to Apache MyFaces" book, Hazem is also an author of many technical articles, a developerWorks contributing author and a technical speaker in both local and international conferences such as ApacheCon North America, Geecon, JavaLand, JSFDays, CON-FESS Vienna and JavaOne. Hazem is an XIBMer, he worked in IBM for ten years. Now, He is working for Nickelodeon New York as a Mobile Architect. He is also an OpenGroup Master Certified Specialist.

4 thoughts on “Getting the path of WEB-INF folder from a Java Servlet

  1. Sorry, but on Weblogic, for example, this method doesn’t work! It is not always supported by vendors and it isn’t a reliable approach.

    • With all due respect.
      No, This API has to be supported from all the vendors (unless the Servlet is accessing a remote system NOT THE LOCAL FILE SYSTEM of the Servlet). This API is part of the Servlet specification:
      “The getRealPath method takes a String argument and returns a String representation of a file on the local file system to which a path corresponds… In situations where the servlet container cannot determine a valid file path for these methods, such as when the Web application is executed from an archive, on a remote file system not accessible locally, or in a database, these methods must return null.”[1]


      • You are correct in that the API must be supported by all vendors. However, you must be prepared for it to return null. It may behave as you expect in a (typically exploded) development environment and fail in a production environment.

        The safe way to read WEB-INF resources is to use ServletContext.getResourceAsStream(…).

        For similar reasons you should not expect to be able to write content into your web app either. Think about what would this would do in a cluster…

Comments are closed.