This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Customer Requirements for Developers and have Marcho Behler on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes Accessing xslt files from EJB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Customer Requirements for Developers this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Accessing xslt files from EJB" Watch "Accessing xslt files from EJB" New topic

Accessing xslt files from EJB

Angus Rose
Ranch Hand

Joined: Jun 21, 2002
Posts: 56
Hi All,
I'm currently writing an EJB that needs to transform an xml file using xslt. It does this using another object that actually does the transforming process. What I need to know is where to place the xml/xslt files in the EAR file as the xslt file may change at a later date and so I don't want to wrap it up in a jar file if at all possible.

Thanks in advance

Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 32330

I recommend placing the path to the XSLT file in a property file. That way, you can change the path later on when/if a different XSLT file is needed.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Angus Rose
Ranch Hand

Joined: Jun 21, 2002
Posts: 56
where would I place the properties file though - within the ejb-jar or elsewhere within the .ear file? I need to do it this way so that I can use a relative URL.

Angus Rose
Ranch Hand

Joined: Jun 21, 2002
Posts: 56
could you supply a code snippet to show how I would access a properties file named that is placed in a dir named 'properties' which in turn resides under the enterprise app root dir. This access is from an ejb running in oc4j.

Thanks in advance

Valentin Tanase
Ranch Hand

Joined: Feb 17, 2005
Posts: 704
Hi Rose,

You need to make sure that the classpath contains the (check your container classloading architecture). This is a quick and dirty solution and there are several problems with this approach, since different containers have different classloading strategies. It also could result in very subtle bugs if different versions of the same file are part of the classpath, etc. This are few of the reasons why people prefer using the java.util.ResourceBundle. I personally used the getResourceAsStream approache several times now and I learned how to leave with it :-) Unfortunately for the latest I have no code samples to share with you, but I�m pretty sure you might find some good examples on the net.

I think, therefore I exist -- Rene Descartes
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
I doubt whether any of this should be done in an EJB container. An EJB should deal with objects. I would normally expect an EJB to return a transfer object which a servlet would marshall to XML for rendering with an XSLT stylesheet. After all, this is stuff for the presentation layer, not the business logic layer.

You will still build and deploy an EAR file containing the servlet and EJB code.

SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Rusty Enisin
Ranch Hand

Joined: May 26, 2005
Posts: 107

Instead of doing this with loading a property file:

I cut out the whole property thing by doing this:

You won't have to change a property file when your deployment environment changes. You cut out the step of having to read a property file just to find the path (then you have to go load that too). You can put your xsl in your package structure and manage it with your code.

I use a package structure like this: com.mycompany.resources.xsl. Then I put my xsl files in there.

If you are doing J2EE development with EJBs you will need to add each xsl file to the ejb-jar as an external resource. Then they will show up in the package structure.


The squeaky wheel gets the grease. Well, that or replaced...
It is sorta covered in the JavaRanch Style Guide.
subject: Accessing xslt files from EJB
It's not a secret anymore!