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 / First Aid / May 2004

Tip: Looking for answers? Try searching our database.

I/O using servlets

Thread view: 
Andy - 16 May 2004 19:16 GMT
I'm trying to make a little servlet that takes a small xml file,
passes it to a Stylizer class to transform it into, say HTML, then
output the HTML.  The problem I'm having is that however I describe
the file, I can't get the program to find it.

H:
  MyServlet
      WEB-INF
           Classes
             WebApp.java
             Stylizer.java
           lib
             data.xml
             style.xsl

FileReader fr = new FileReader("H://MyServlet/WEB-INF/lib/data.xml");

With the file structure above, I tried to refer an xml file as shown,
using a whole bundle of paths none of which worked.  I kept getting
the error.

java.io.FileNotFoundException:
C:\local\netbeans3.5.1\netbeans3.5.1\bin\[whatever path I put in]

Could someone please point out my errors.

More generally, does anyone know of an online tutorial (or even a good
book) that has information about how servlets deal with file paths
etc.  This is an area that I find difficult and would like to improve
on (I seem to spend more time trying to solve problems of this nature
than I do solving application problems :-( ).

Thanks for your time.

Andy
Andrew Thompson - 16 May 2004 19:45 GMT
> FileReader fr = new FileReader("H://MyServlet/WEB-INF/lib/data.xml");

Try instead..
URL url = getServletContext().getResource("/WEB-INF/lib/data.xml");
// do as you will with the URL
out.print(url);
File f = new File( url.getFile() );
...

Note well: It is extremely dangerous
to allow things to be drawn straight
out of WEB-INF for display, you should
only allow it for resources that you
specifically identify.

HTH

Signature

Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology

Andy - 17 May 2004 20:49 GMT
> Try instead..
> URL url = getServletContext().getResource("/WEB-INF/lib/data.xml");

Aha :-) that worked beautifully.  Thankyou very much.  I even found
the getResourceAsStream() method while I was there which suits the
problem better.

> Note well: It is extremely dangerous
> to allow things to be drawn straight
> out of WEB-INF for display,

Why is that?

> you should
> only allow it for resources that you
> specifically identify.

So would the WEB-INF be an appropriate location to store an xml file
that contained site content (which was to be modified using xsl).  Or
are you saying that these type of files should be stored independantly
of the servlet and that the lib should only be used for data that the
servlet was dependant on for its execution

Andy


> HTH
Andrew Thompson - 18 May 2004 06:49 GMT
>> Note well: It is extremely dangerous
>> to allow things to be drawn straight
>> out of WEB-INF for display,
>
> Why is that?

Dangreous only in terms of security..

Would you want your visitors to be able to casually display..
http://my.com/display/WEB_INF/classes/security/PassWords.java  ?

;-)

Signature

Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology



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



©2008 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.