Open xml file from jsp custom tag

D

DBoy001

Hi!

I have a custom jsp tag inside wich I want to open an xml file (using
JDOM) to retreive some information. The xml file is located in the
same folder as the jsp using it.

Here's an example:

public int doEndTag() throws JspException {
JspWriter out = this.pageContext.getOut();

SAXBuilder sxb = new SAXBuilder();
Document xmlDoc = sxb.build(new File("params.xml"));
...

If I do it like that, it will search for the file in the application
server's "bin" directory... is there a way to retreive the file in the
right place (using pageContext I guess)?

Thanks
 
M

Manish Pandit

Hi!

I have a custom jsp tag inside wich I want to open an xml file (using
JDOM) to retreive some information. The xml file is located in the
same folder as the jsp using it.

Here's an example:

public int doEndTag() throws JspException {
JspWriter out = this.pageContext.getOut();

SAXBuilder sxb = new SAXBuilder();
Document xmlDoc = sxb.build(new File("params.xml"));
...

If I do it like that, it will search for the file in the application
server's "bin" directory... is there a way to retreive the file in the
right place (using pageContext I guess)?

Thanks

You can put the XML file anywhere in the classpath, and then use:

getClass().getResourceAsStream("/params.xml") which will return the
InputStream to params.xml, that can be passed to the build() method.

-cheers,
Manish
 
D

DBoy001

You can put the XML file anywhere in the classpath, and then use:

getClass().getResourceAsStream("/params.xml") which will return the
InputStream to params.xml, that can be passed to the build() method.

-cheers,
Manish- Masquer le texte des messages précédents -

- Afficher le texte des messages précédents -

Thanks for the answer. I've tried that and it works... only if the
file is in one of the folders in the JVM classpath and that's really
not an option because this classpath is common for all my server
instances and applications... If I was to do that, it would mean I
would need to use the same xml file for production and development...
not an option!

So I'm back to nothing... the solution would be to use the
request.getPathTranslated() function and use the resulting path to
open my file, but the damn thing won't work and always returns null!!!

Getting very frustrated! :) Is anyone using that getPathTranslated
thing and getting any problem?

Thanks!
 
L

Lew

Thanks for the answer. I've tried that and it works... only if the
file is in one of the folders in the JVM classpath and that's really
not an option because this classpath is common for all my server
instances and applications... If I was to do that, it would mean I
would need to use the same xml file for production and development...
not an option!

So I'm back to nothing... the solution would be to use the
request.getPathTranslated() function and use the resulting path to
open my file, but the damn thing won't work and always returns null!!!

What's wrong with using the application-specific part of the classpath for the
application-specific files?
 
D

DBoy001

What's wrong with using the application-specific part of the classpath for the
application-specific files?

It wouldn't be wrong, but it doesn't work... I tested different
locations and it'll only work with the directories that are on the JVM
classpath (I wasn't clear on that part, sorry)...

ANYWAYYYY, I found a solution for getting my getPathTranslated,
"manually" translated... here it goes :

String xmlFileWithPath =
this.pageContext.getServletContext().getRealPath(request.getServletPath());
xmlFileWithPath = xmlFileWithPath.substring(0,
xmlFileWithPath.lastIndexOf("\\"));
xmlFileWithPath = xmlFileWithPath + "\\menu.xml";

SAXBuilder sxb = new SAXBuilder();
Document xmlDocument = sxb.build(new
File(xmlFileWithPath));

Thanks for your help!
 
L

Lew

It wouldn't be wrong, but it doesn't work... I tested different
locations and it'll only work with the directories that are on the JVM
classpath (I wasn't clear on that part, sorry)...

What locations did you test?

Is this a web application?

If so, did you put the JAR in WEB-INF/lib/?
 
D

DBoy001

What locations did you test?

Is this a web application?

If so, did you put the JAR in WEB-INF/lib/?

No, it's not a java web application, it's a ColdFusion-hybrid
application that uses custom jsp tags and javabeans... so it's kind of
like all my webapps are actually sharing the same webapp space thus,
forcing me to identify their location... it's kind of bulsh**ty but
I'm stuck with that and trying to make something good out of it!

Anyway, the solution I put up here solves my problem perfectly so I'm
good, until next time :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top