I have a web app and I need to get absolute path of a folder (in my case, WEB-INF) in a static block of a class which is not a servlet. Of course, I could take this value from properties file but can I do this otherwise?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use the getRealPath() method of the ServletContext ServletContext.getRealPath to find out the actual folder in your filesystem like
You may not get the WEB-INF folder name like this because it is not in the servlet context’s exposed directories, you may have to do something like this for it
[EDIT] If you dont have the
requestobject, then you will have to use aServletContextListenerand use itscontextInitialized(ServletContextEvent sce)method to grab theServletContextand store the path into the application scope for retrieving it later.