- Posted by: kentkong
- Posted on: December 03 2002 07:47 EST
I meet a problem.
We know if you develop a MVC project, the control xml can be put into \WEB-INF\ ,you can use the ServletContext.getResource(String path) to get the xml url, note the path is a relative path to the WebApp root. This means you can zip the whole web app into a war file, deploy it everywhere , needn't reconfigure, cause the XML can be accessed by relative path.
Now one of my ejb need access a config file, a simple xml, the ejb works as the controller in the MVC pattern. but the ejb api doesnot provide the similar interface ,you cannot access resources with a relative path. I set an ejb environment variable in ejb-jar.xml to store the xml file path, but when I deploy the ejb in different machine , j2ee server or os ,I nearly reconfigure the xml file path every time. I
How can I package the xml file and ejb files into one jar file , and make it work?
Or do you know a better solution?
- The problem when using a session bean as controller by Matthew Cox on December 03 2002 12:03 EST
- The problem when using a session bean as controller by kentkong on December 06 2002 23:27 EST
This is a great question, I'm assuming removeing the config file from the packaged file is not an option for you. I had the same one a while ago and decided to simply load the xml file in our servlet and populate an object which was then available.
I guess you could discover the config file location in the servlet and then set a singleton which contained the file location. Your ejb should then be able to load it.
You can use the ClassLoader to load not only classes but other files as well as long as they are in the classpath. So by adding the config xml file to you ejb classpath you should be able to look for it with default loader.
I use the Class.getResource(String path) to solve the problem, the method return a URL object, then I can read the file, and the path is relative path to the class which calls the getResource() method.