Using tomcat 6, i created a template inside www/templates/templatefile.html
how do I access it from a servlet ? i want to read the html file and parse it.
I tried getting the real path using request.getRealPath(request.getServletPath())
and from there to go to the templates directory but for some reason it still can’t find the file.
Assuming that
wwwis a folder in the public web content root, then you can useServletContext#getRealPath()to convert a relative web path to an absolute disk file system path as follows:Note that this won’t work when the WAR is not expanded (Tomcat in default trim does it, but it can be configured not to do that). If all you want to end up is having an
InputStreamof it, then you’d rather like to useServletContext#getResourceAsStream()instead.