Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2007

Tip: Looking for answers? Try searching our database.

Open xml file from jsp custom tag

Thread view: 
DBoy001 - 29 Nov 2007 18:18 GMT
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
Manish Pandit - 29 Nov 2007 21:58 GMT
> Hi!
>
[quoted text clipped - 16 lines]
>
> 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
DBoy001 - 05 Dec 2007 14:44 GMT
> > Hi!
>
[quoted text clipped - 26 lines]
>
> - 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!
Lew - 05 Dec 2007 15:26 GMT
Manish Pandit wrote:
>> 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.

> 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
[quoted text clipped - 6 lines]
> 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?

Signature

Lew

DBoy001 - 05 Dec 2007 15:40 GMT
> Manish Pandit wrote:
> >> You can put the XML file anywhere in the classpath, and then use:
[quoted text clipped - 19 lines]
>
> - Afficher le texte des messages précédents -

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!
Lew - 05 Dec 2007 19:16 GMT
Lew wrote:
>> 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)...

What locations did you test?

Is this a web application?

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

Signature

Lew
this post contained three requests for information.

DBoy001 - 06 Dec 2007 15:55 GMT
> Lew wrote:
> >> What's wrong with using the application-specific part of the classpath for the
[quoted text clipped - 12 lines]
> Lew
> this post contained three requests for information.

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 :-)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.