2

I looking for something like of request.getServletContext().getRealPath() to use inside custom class (interceptor in Hibernate, which hasn't access to request object).

File file = new File("/photo/test.txt");
file.delete();

... above code will fail, because the path of file is /lib directory of Tomcat now.

But i need to start count from the project, not Tomcat.

Any ideas?

WildDev
  • 2,250
  • 5
  • 35
  • 67
  • 1
    `/photos` would be from root of file system, what you mean by count from the project – jmj Jul 03 '14 at 17:16
  • I mean to make the path relative to root of my site, jsp project in this case – WildDev Jul 03 '14 at 17:19
  • possible duplicate of [Is there a way to get the absolute path of the context root in tomcat?](http://stackoverflow.com/questions/5100182/is-there-a-way-to-get-the-absolute-path-of-the-context-root-in-tomcat) – Nir Alfasi Jul 03 '14 at 17:19
  • @alfasin, seems like no. My webapp deployed not in tomcat instance, could it to be the cause? – WildDev Jul 03 '14 at 17:23
  • getRealPath will return the path from the root and beyond. You should use the context path, instead. Take a look at [this thread](http://stackoverflow.com/questions/5850336/what-does-this-expression-language-pagecontext-request-contextpath-exactly-do) – RafaelTSCS Jul 03 '14 at 17:40
  • 1
    It would be nice when asking for help to give all relevant information in the question ! I suppose you have no access to request only because of a comment to an answer. Because in Spring, interceptors do have access to request and response ... Could you at least confirm (or reject ?) that and if possible update your question with it. Because there are of course other solutions depending on what is your need. – Serge Ballesta Jul 03 '14 at 18:28
  • @SergeBallesta, i talking about Hibernate's intercepters. I didn't know they are exists somewhere else, so i didn't specify it. Now i did. Rest info are enough to understand my prob i think. – WildDev Jul 03 '14 at 18:48

2 Answers2

2

getRealPath will return the path from the root and beyond.

You should use the context path, instead.

Take a look at this thread

try to use

request.getServletContext().getContextPath();
Community
  • 1
  • 1
RafaelTSCS
  • 1,234
  • 2
  • 15
  • 36
  • Please edit your question to provide the useful parts of the other Q/A in your answer (it's not a thread in this site). Link only answers are not accepted here. – Luiggi Mendoza Jul 03 '14 at 17:31
  • They are actually related to the same issue. Is there any specific way to link stack threads to answers? I'm editing it. – RafaelTSCS Jul 03 '14 at 17:39
  • guys i told you i looking for something like you mentioned to use inside class aka interceptor. I can't pass request parameter to this in several causes, so i would to know is there any another solutions. – WildDev Jul 03 '14 at 18:12
2

IMHO the simplest way for you is to get the root of your application in a ServletContextListener declared in web.xml or via a WebListener annotation and to store it in a static variable.

public class RootPathHolder implements ServletContextListener {
    static String rootPath;

    public void contextInitialized(ServletContextEvent sce) {
        RootServletListener.rootPath = sce.getServletContext().getRealPath("/");
    }

    public void contextDestroyed(ServletContextEvent sce) {
    }

    public static String getRootPath() {
        return rootPath;
    }
}

That way you will have access to the root path of your application from anywhere as RootPathHolder.getRootPath() and in your interceptor you will only need to do a path concatenation :

Path realPath = Paths.get(RootPathHolder.getRootPath(), "photo/test.txt");
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252