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 / November 2006

Tip: Looking for answers? Try searching our database.

reading a file from WEB-INF?

Thread view: 
Jim Bancroft - 18 Nov 2006 08:01 GMT
I have some text files I'd like to put on my web server (preferably in the
WEB-INF directory though they could be in a regular web folder) that I will
read and parse from some of my servlets.  Does anyone have a code snippet
from doing this from within, say, my servlet's init() method?  The parsing
bit I've already done; what I'm having trouble with is figuring out how to
get absolute paths that I can use in my Buffered reader.

Here's what I've got so far...

           ServletContext context = getServletContext();
           String realContextPath = context.getRealPath("/");
           s = new Scanner(new BufferedReader(new FileReader(
"mapping1.txt")));

For some reason calling getRealPath("/") above returns something unusual;
instead of "mypath\web" I get "mypath\build\web".  Not sure where that
"build" bit is coming from or if it matters.  Probably the result of my
compilation, with mapping1.txt copied in along with the rest of the web app?

Also, what happens if I deploy my final product as a jar file?  Can I still
read files from WEB-INF, or another directory within the web server, just
like if I had deployed an uncompressed set of folders?
仁者无敌 - 18 Nov 2006 08:42 GMT
I think you can put your .txt file in your classpath,
in a folder or in a jar.
In some package,
e.g

in    com/xxx/

Then you can use the code:
public class XXXServlet extends HttpServlet{
     ....
    void mymethod(){
         s=new BufferedReader(new InputStreamReader(

XXXServlet.class.getResourceAsStream("/com/xxx/yourtxt.txt")));
                  // here "/" means the root of your classpath,
                  //you can get  a platform independent  path now ;-)
    }
}

> I have some text files I'd like to put on my web server (preferably in the
> WEB-INF directory though they could be in a regular web folder) that I will
[quoted text clipped - 18 lines]
> read files from WEB-INF, or another directory within the web server, just
> like if I had deployed an uncompressed set of folders?
riccardonews@gmail.com - 18 Nov 2006 08:51 GMT
Hello,
if you create a file (readme.txt for example) into folder
"WEB-INF/classes/commons",
use this example code for reading it.:

InputStream in =
this.getClass().getClassLoader().getResourceAsStream("commons/readme.txt");

bye bye
Riccardo.


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.