views:

78

answers:

4

I want to add a xml file to my Java ee project and use it in my code but when I address the file from src directory it does not understand my address and search for file in bin directory of tomcat. My project is using wicket framwork and JavaEE. does any one know how to address the file or where should I place the file to access is from project?

A: 

Inside your servlet you can:

this.getServletContext().getResource( path );

public java.net.URL getResource(java.lang.String path)
                         throws java.net.MalformedURLException

Returns a URL to the resource that is mapped to a specified path. The path must begin with a "/" and is interpreted as relative to the current context root.

This method allows the servlet container to make a resource available to servlets from any source. Resources can be located on a local or remote file system, in a database, or in a .war file.

The servlet container must implement the URL handlers and URLConnection objects that are necessary to access the resource.

This method returns null if no resource is mapped to the pathname.

Some containers may allow writing to the URL returned by this method using the methods of the URL class.

The resource content is returned directly, so be aware that requesting a .jsp page returns the JSP source code. Use a RequestDispatcher instead to include results of an execution.

This method has a different purpose than java.lang.Class.getResource, which looks up resources based on a class loader. This method does not use class loaders.

Peter D
give credit/link where it's due: http://java.sun.com/products/servlet/2.3/javadoc/javax/servlet/ServletContext.html#getResource(java.lang.String)
Zack
its JEE, do we really need to say where it comes from? The answer is helpful and relevent, doesn't derserve a downvote
djna
but it's just copied and pasted from the source, without any reference to it.
Zack
The source and reference can be found in 5 seconds. I was providing an answer.
Peter D
A: 

Normally src is not used at runtime. I don't use tomcat, but in my environment I have build tasks that copy non-Java files from src to the bin directory.

You may find it necessary to open the file by using the classloader getResourceAsStream() so that you are searching the same directories as your classes come from.

This assumes that you want to deliver the XML in the application. Alternatively you may prefer to have it somewhere else. URI resource references may help

djna
A: 

Why don't you place the XML file in your resources directory. You can access it from any class with:

MyClass.class.getClassLoader().getResourceAsStream(filename);

amischiefr
+1  A: 

If your xml file is a resource that must be accessed server-side only, then the best choice is to place it in the WEB-INF directory inside your war, or in some subdirectory inside the WEB-INF. This way you ensure the resource will not be accessible by the web.

Then you can retrieve it using ServletContext.getResource, as pointed out by Peter D.

For example, in a servlet you can retrieve it this way (exception handling omitted):

String path = "/WEB-INF/my.xml";
URL url = getServletConfig().getServletContext().getResource(path);
InputStream in = url.openStream();
// read content from input stream...
Massimiliano Fliri