Re: jsp - file location

Discussion in 'Java' started by Nikolaj, Jul 4, 2003.

  1. Nikolaj

    Nikolaj Guest

    But how do I access the files from my java-code if they are located in
    the web-inf dir. I have tried to have a jsp page save a file using a
    simple FileWriter:
    FileWriter fw = new FileWriter(new File("testFile"));
    fw.write("hello");

    The file ends up in a directory called Tomcat located in the directory
    of my web-application. (I'm developing in JBuilder). This doesn't seem
    very consistent, and how would I make this work on other servers if
    the web-application should be able to be deployed anywhere..
    How do I find the web-inf directory when I don't know the absolute
    path?

    Should I use URLs instead of files and paths.

    Hope someone can help me. Thanks.

    Nikolaj



    "Andy Flowers" <> wrote in message news:<eIlJa.1812$>...
    > In the web-inf directory.
    >
    > "Nikolaj" <> wrote in message
    > news:...
    > > I am developing an internet site based on jsp.
    > > The content of the site is a number of articles, which exist as plain
    > > unformatted text.
    > > I want the "front page" of the site to contain the headline of each
    > > article, and the first 15 words followed by "..."
    > > This should be done by automatically generating the html using java.
    > > The java-code should read the textfile and dynamically generate the
    > > front page, based on the articles currently available.
    > >
    > > Further more each hedline should be a link to the corresponding
    > > article, which should be a html page generated automatically from the
    > > text file containing the article.
    > >
    > > And now the question:
    > > Where should I locate the text files, which contains the unformatted
    > > articles. I do not want the text files to be accessible from a
    > > browser. Only the formatted versions of the articles should be
    > > accessible.
    > >
    > > I know that web applications have a directory which is used for
    > > storing temporary files, but it doesn't seem like a good solution to
    > > use this directory, since the files are not to be considered
    > > temporary.
    > >
    > > Hope you can help me. Thanks.
     
    Nikolaj, Jul 4, 2003
    #1
    1. Advertising

  2. Nikolaj

    Andy Flowers Guest

    In your JSP use the API.

    The ServletContext class, surfaced as the application pre-defined JSP
    variable, is what you want.

    A call to application.getRealPath("WEB-INF"); in your JSP will return the
    full operating system path to the WEB-INF directory in a J2EE application.

    There are also other useful methods in this calls, such as getResource(...),
    that you may want to investigate as well.

    The reason the simple FileWriter test you have done ends up in the directory
    you specified is that this is where the JVM root is and this is the root
    where the JVM will look for files with no qualifiers. Have a look at the
    Java file APIs for descriptions of this.

    "Nikolaj" <> wrote in message
    news:...
    > But how do I access the files from my java-code if they are located in
    > the web-inf dir. I have tried to have a jsp page save a file using a
    > simple FileWriter:
    > FileWriter fw = new FileWriter(new File("testFile"));
    > fw.write("hello");
    >
    > The file ends up in a directory called Tomcat located in the directory
    > of my web-application. (I'm developing in JBuilder). This doesn't seem
    > very consistent, and how would I make this work on other servers if
    > the web-application should be able to be deployed anywhere..
    > How do I find the web-inf directory when I don't know the absolute
    > path?
    >
    > Should I use URLs instead of files and paths.
    >
    > Hope someone can help me. Thanks.
    >
    > Nikolaj
    >
    >
    >
    > "Andy Flowers" <> wrote in message

    news:<eIlJa.1812$>...
    > > In the web-inf directory.
    > >
    > > "Nikolaj" <> wrote in message
    > > news:...
    > > > I am developing an internet site based on jsp.
    > > > The content of the site is a number of articles, which exist as plain
    > > > unformatted text.
    > > > I want the "front page" of the site to contain the headline of each
    > > > article, and the first 15 words followed by "..."
    > > > This should be done by automatically generating the html using java.
    > > > The java-code should read the textfile and dynamically generate the
    > > > front page, based on the articles currently available.
    > > >
    > > > Further more each hedline should be a link to the corresponding
    > > > article, which should be a html page generated automatically from the
    > > > text file containing the article.
    > > >
    > > > And now the question:
    > > > Where should I locate the text files, which contains the unformatted
    > > > articles. I do not want the text files to be accessible from a
    > > > browser. Only the formatted versions of the articles should be
    > > > accessible.
    > > >
    > > > I know that web applications have a directory which is used for
    > > > storing temporary files, but it doesn't seem like a good solution to
    > > > use this directory, since the files are not to be considered
    > > > temporary.
    > > >
    > > > Hope you can help me. Thanks.
     
    Andy Flowers, Jul 4, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luke Dalessandro
    Replies:
    0
    Views:
    633
    Luke Dalessandro
    Jan 15, 2006
  2. Replies:
    2
    Views:
    1,126
    Ferenc Hechler
    Dec 16, 2004
  3. Replies:
    0
    Views:
    356
  4. Replies:
    0
    Views:
    4,504
  5. Replies:
    8
    Views:
    3,674
    Manish Pandit
    Oct 11, 2006
Loading...

Share This Page