aspose file tools*
The moose likes Beginning Java and the fly likes Web application path Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Web application path" Watch "Web application path" New topic
Author

Web application path

samantha clarkson
Ranch Hand

Joined: Sep 09, 2008
Posts: 56
Hello everybody,

how to get the path of my web application,
for example im working on eclipse and the name of my web application is test which is in the desktop; i want in my javabean get the path : "c:\Documents and Settings\user\workspace\test", how we can do this in java, in my javabean which i can't extend it to the HttpServlet class.

im going crazy

Thanks in advance.


farm rubbit hihihihihihi, be aware !!
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4176
    
  21

You have to get it from the ServletContext using servletContext.getRealPath() then pass it into the JavaBean.

You said that your JavaBean can't be turned into a Servlet, and you are definitely correct in that it should not be. And if you can you should try to keep the Bean independent of being used in a Servlet container, which will give you more flexibility. But this means you have to initialize the bean with the values which only the environment would know, such as the real path to the application. If you find you have a lot of such properties then I would consider generating an 'environment context' that provides access to these values.


Steve
samantha clarkson
Ranch Hand

Joined: Sep 09, 2008
Posts: 56
Hello,

Why im asking this in the first post, just to can use an xml file which i need to keep it in my work directory, but there are other people who develop so each one has the workspace in his privileged location, so the link to the xml file must be generic.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4176
    
  21

Originally posted by jad igor:
Hello,

Why im asking this in the first post, just to can use an xml file which i need to keep it in my work directory, but there are other people who develop so each one has the workspace in his privileged location, so the link to the xml file must be generic.


Not sure I understand what you are saying... Did my last post help? If not can you give a detailed explanation of what you want to happen and why?
samantha clarkson
Ranch Hand

Joined: Sep 09, 2008
Posts: 56
Hello,

first thank you for your answers, but i didn't understand exactly what do you mean. well i have a singleton which read data from an xml file (using DOM), i have a javabean which store data provided from the singleton in an arraylist, this arraylist populate a list in my jsf file. in the singleton i put the path of the xml file statically, the problem is make this path dynamically so other people can develop my code without modifying each time the path.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4176
    
  21

Where is the file located relative to the Singlton Class?

There are two ways to approach this. The first is the method you suggested in your first post. You would need to find the base file location for the web application, but this can only be provided by the Server, the Singlton can't detect it. So you have some servlet or object in the Server which can access the ServletContext object and calls and then tells the Singleton Class where the XML file is located.

The second approach is to access the XML file in a relative location to the Singleton Class. You would use or

The second approach works well if the XML file is located in the same class loader as the Singleton (meaning packed in the same JAR, or stored in the WEB-INF/classes/ directory) and each user gets their own Singleton class and XML file.

The first approach works well if the users share a single Singleton object, or if the XML file is stored in the web path (not inside the WEB-INF/classes/ or WEB-INF/lib directories) but makes the application setup fairly confusing.
samantha clarkson
Ranch Hand

Joined: Sep 09, 2008
Posts: 56
Hi Steve,

i've tried the first approach,
String xmlLocation = servletContext.getRealPath("applicationDescriptor.xml");
the output is :
/home/user/eclipse/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/myproject/applicationDescriptor.xml

But

this is a temporary directory and the xml file is not there, my file is in :
/home/user/eclipse/workspcae/myproject/applicationDescriptor.xml

the second approach :

i've added a resource directory "resources" using the eclipse wizard in my src folder and i've placed my xml file in it, but it returns null :
URL sc = this.getClass().getResource("/resources/applicationDescriptor.xml");

Thank you for your help.

[ September 12, 2008: Message edited by: jad igor ]
[ September 12, 2008: Message edited by: jad igor ]
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4176
    
  21

Originally posted by jad igor:
Hi Steve,

i've tried the first approach,
String xmlLocation = servletContext.getRealPath("applicationDescriptor.xml");
the output is :
/home/user/eclipse/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/myproject/applicationDescriptor.xml

But

this is a temporary directory and the xml file is not there, my file is in :
/home/user/eclipse/workspcae/myproject/applicationDescriptor.xml


Not exactly. The workspace version of the applicationDescriptor is found in the workspcae/myproject/ directory. This would be your 'development version' and would be akin to the .java files for a class. The version of the XML that will be used when the server is started from inside Eclipse will be the first path. This would be your test deployment location and would act like a .class file you are testing. The path that will be used if you deploy your application as a WAR directly to a server would be something different, and would be deployment location.

You don't want to tie the XML location to your development path. You DO want to make it reflect the deployment path, which is exactly what this does.

So the question is, is there something that makes using the deployment location not work for you?



Originally posted by jad igor:
the second approach :

i've added a resource directory "resources" using the eclipse wizard in my src folder and i've placed my xml file in it, but it returns null :
URL sc = this.getClass().getResource("/resources/applicationDescriptor.xml");

Thank you for your help.


Double check that the resources path is published to your web server. It may take a restart of the server. Also be sure that the class loading the resource is in the same class loader as the XML file. For example, the class should not be in a JAR in the application's /lib directory unless the XML is in the same place, the class should not be in a common or shared library unless the XML is also in the same common or shared library.

Note that this will provide the same sort of results as the getRealPath, when you get it to work you will get something like:
/home/user/eclipse/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/myproject/WEB-INF/classes/resources/applicationDescriptor.xml
Which will be the deployment location for resources.applicationDescriptor.xml.
[ September 13, 2008: Message edited by: Steve Luke ]
samantha clarkson
Ranch Hand

Joined: Sep 09, 2008
Posts: 56
Hello Steve,

Before i've addes a folder called resources in my src folder, i've see all the resource directories declared in the context some folders were known, but the one that i added was not i'don't know why.
So i've added my xml file into one of those known folders and i checked the location of temporary deployement and i've seen my xml file.

FacesContext context = FacesContext.getCurrentInstance();
ServletContext servletContext = (ServletContext)context.getExternalContext().getContext();
String xmlLocation = servletContext.getRealPath("/images/applicationDescriptor.xml");

now it works fine.
Thank you for your help.

 
wood burning stoves
 
subject: Web application path