users@glassfish.java.net

Re: How do I access a file in .war from compiled jsp?

From: Jan Luehe <Jan.Luehe_at_Sun.COM>
Date: Wed, 03 Feb 2010 09:22:36 -0800

On 02/ 3/10 08:01 AM, David Sanmartín wrote:
> Sorry JO, but my idea only works with Struts.
>
> On Wed, Feb 3, 2010 at 16:53, Sigal Shaharabani
> <Sigal.Shaharabani_at_teledata-networks.com
> <mailto:Sigal.Shaharabani_at_teledata-networks.com>> wrote:
>
> To access a file which is in my archive I usually use the method
> getResourceAsStream which is part of java.lang.Class
>

The above will work only if the requested resource is part of the
application's
classpath, that is, contained inside the WAR file's WEB-INF/classes
folder or
inside a JAR file inside WEB-INF/lib.

I believe that in JO's case, the requested resource is stored inside the
WAR file's
document root, in which case it would not be available from the
application's
classpath.

In this case, you can access it via ServletContext#getResource or
ServletContext#getResourceAsStream.

 From within a JSP page, the ServletContext object is available via the
implicit variable with name "application".

Hope this helps.

Jan

>
> -----Original Message-----
> From: glassfish_at_javadesktop.org <mailto:glassfish_at_javadesktop.org>
> [mailto:glassfish_at_javadesktop.org <mailto:glassfish_at_javadesktop.org>]
> Sent: Wednesday, February 03, 2010 5:01 PM
> To: users_at_glassfish.dev.java.net <mailto:users_at_glassfish.dev.java.net>
> Subject: How do I access a file in .war from compiled jsp?
>
> First Post.
>
> Hello all, I'm new with both Glassfish and jsp programming so bear
> with
> me if I seem to be missing something elementary.
>
> What I have is a simple application consisting of a jsp file that
> needs
> to access an XML document for editing. This document is stored along
> with the jsp in a WAR file that is deployed as a web application on
> glassfish.
>
> I can access the XML file directly by going to
> [i]http://localhost:8080/app_name/data/xmlFile.xml[/i]
> <http://localhost:8080/app_name/data/xmlFile.xml%5B/i%5D>, which
> is fine if
> I want to display data from the XML file; but I need to edit it
> directly.
>
> In order to do this it looks like I need to open it as a java File
> object and blah blah (I can make that work), however, opening it via a
> URL doesn't seem like the way to do that.
>
> This brings me to my question: how can I access the file directly (ie
> "C:/glassfish/domains/domain1/.../data/xmlFile.xml". this shouldn't be
> too hard, except that the data/xmlFile.xml is in a WAR file.
>
> I can get to the file using javascript by just going to
> "../data/xmlFile.xml".
>
> Let me know if you need any more clarification.
>
> Thanks in advance for any and all help!
>
> JO
> [Message sent by forum member 'jayo3434' (jayo3434_at_yahoo.com
> <mailto:jayo3434_at_yahoo.com>)]
>
> http://forums.java.net/jive/thread.jspa?messageID=384516
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> <mailto:users-unsubscribe_at_glassfish.dev.java.net>
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
> <mailto:users-help_at_glassfish.dev.java.net>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> <mailto:users-unsubscribe_at_glassfish.dev.java.net>
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
> <mailto:users-help_at_glassfish.dev.java.net>
>
>
>
>
> --
> David Sanmartín
> www.david-sanmartin.net <http://www.david-sanmartin.net>
> Sent from Valencia, Comunidad Valenciana, España