Skip to main content

How to get the current path of my web application

3 replies [Last post]
mediii
Offline
Joined: 2007-09-03

hello

i need to get the current path of my war file from glassfish in java source code, how can i do that ??

I use jsf application and i need to load a file located in my root folder of my war

with this code i get only the context name :/

FacesContext context = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();

String path = request.getContextPath();

but how can i load my file

thanks a lot

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
yonestar

I had a situation where i needed to load a file from the domain's docroot
(i.e. glassfish/domains/domain1/docroot)

This code provides the path of the docroot, and then you can just append the
file name:

/**
* Get path of the docroot, regardless of where project is deployed
* @return docroot path
*/
private static String getDocRoot() {
File docroot1 = new File("../docroot");

try {
docroot1 = docroot1.getCanonicalFile();
} catch (IOException e) {
docroot1 = docroot1.getAbsoluteFile();
}
return docroot1.getPath() + "\\";
}
--
View this message in context: http://www.nabble.com/How-to-get-the-current-path-of-my-web-application-...
Sent from the java.net - glassfish users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net

jluehe
Offline
Joined: 2004-12-01

Have you tried using ServletContext.getRealPath()?

mediii
Offline
Joined: 2007-09-03

thanks its done with

FacesContext context = FacesContext.getCurrentInstance();
ServletContext sc = (ServletContext) context.getExternalContext().getContext();

String path = sc.getRealPath("myfile"); when myfile is located in the root of my webapplication