Accessing Server File with Servlets

Discussion in 'Java' started by Chase Preuninger, Sep 6, 2009.

  1. I have a bunch of javascript files contained in an xyz.war file on my
    JBoss server. I need a way using servlets (not JSP) to load them into
    memory using InputStreams, process them and then send them back out to
    the client's web browser. My problem is I can't figure out how to
    access these files through a servlet I also want to keep it as dynamic
    as possible since I don't know where this code may end up.
    Chase Preuninger, Sep 6, 2009
    #1
    1. Advertising

  2. Chase Preuninger

    Arne Vajhøj Guest

    Chase Preuninger wrote:
    > I have a bunch of javascript files contained in an xyz.war file on my
    > JBoss server. I need a way using servlets (not JSP) to load them into
    > memory using InputStreams, process them and then send them back out to
    > the client's web browser. My problem is I can't figure out how to
    > access these files through a servlet I also want to keep it as dynamic
    > as possible since I don't know where this code may end up.


    If they are in WEB-INF/classes or WEB-INF/lib then you can use
    ClassLoader getResourceAsStream to read them.

    If not then you can try open it as a file via getServletContext
    getRealPath.

    Arne
    Arne Vajhøj, Sep 6, 2009
    #2
    1. Advertising

  3. On Sep 5, 9:06 pm, Arne Vajhøj <> wrote:
    > Chase Preuninger wrote:
    > > I have a bunch of javascript files contained in an xyz.war file on my
    > > JBoss server.  I need a way using servlets (not JSP) to load them into
    > > memory using InputStreams, process them and then send them back out to
    > > the client's web browser.  My problem is I can't figure out how to
    > > access these files through a servlet I also want to keep it as dynamic
    > > as possible since I don't know where this code may end up.

    >
    > If they are in WEB-INF/classes or WEB-INF/lib then you can use
    > ClassLoader getResourceAsStream to read them.
    >
    > If not then you can try open it as a file via getServletContext
    > getRealPath.
    >
    > Arne


    This is the path returned by the getRealPath("") call.

    C:\Program Files\jboss-4.2.0.GA\server\default\.\tmp\deploy
    \tmp3242525634535.war\

    Can't be found and where did the "." come from.
    Chase Preuninger, Sep 6, 2009
    #3
  4. Chase Preuninger wrote:
    > On Sep 5, 9:06 pm, Arne Vajhøj <> wrote:
    >> Chase Preuninger wrote:
    >>> I have a bunch of javascript files contained in an xyz.war file on my
    >>> JBoss server. I need a way using servlets (not JSP) to load them into
    >>> memory using InputStreams, process them and then send them back out to
    >>> the client's web browser. My problem is I can't figure out how to
    >>> access these files through a servlet I also want to keep it as dynamic
    >>> as possible since I don't know where this code may end up.

    >> If they are in WEB-INF/classes or WEB-INF/lib then you can use
    >> ClassLoader getResourceAsStream to read them.
    >>
    >> If not then you can try open it as a file via getServletContext
    >> getRealPath.
    >>
    >> Arne

    >
    > This is the path returned by the getRealPath("") call.
    >
    > C:\Program Files\jboss-4.2.0.GA\server\default\.\tmp\deploy
    > \tmp3242525634535.war\
    >
    > Can't be found and where did the "." come from.


    A handy way of returning the Javascript file is to include it using a
    request dispatcher...this eliminates the InputStream and OutputStream
    mechanics.

    AHS
    Arved Sandstrom, Sep 6, 2009
    #4
  5. On Sep 6, 12:15 am, Arved Sandstrom <> wrote:
    > Chase Preuninger wrote:
    > > On Sep 5, 9:06 pm, Arne Vajhøj <> wrote:
    > >> Chase Preuninger wrote:
    > >>> I have a bunch of javascript files contained in an xyz.war file on my
    > >>> JBoss server.  I need a way using servlets (not JSP) to load them into
    > >>> memory using InputStreams, process them and then send them back out to
    > >>> the client's web browser.  My problem is I can't figure out how to
    > >>> access these files through a servlet I also want to keep it as dynamic
    > >>> as possible since I don't know where this code may end up.
    > >> If they are in WEB-INF/classes or WEB-INF/lib then you can use
    > >> ClassLoader getResourceAsStream to read them.

    >
    > >> If not then you can try open it as a file via getServletContext
    > >> getRealPath.

    >
    > >> Arne

    >
    > > This is the path returned by the getRealPath("") call.

    >
    > > C:\Program Files\jboss-4.2.0.GA\server\default\.\tmp\deploy
    > > \tmp3242525634535.war\

    >
    > > Can't be found and where did the "." come from.

    >
    > A handy way of returning the Javascript file is to include it using a
    > request dispatcher...this eliminates the InputStream and OutputStream
    > mechanics.
    >
    > AHS- Hide quoted text -
    >
    > - Show quoted text -


    Yet again how do I do that and still what does the "." mean?
    Chase Preuninger, Sep 6, 2009
    #5
  6. Chase Preuninger wrote:
    > On Sep 6, 12:15 am, Arved Sandstrom <> wrote:
    >> Chase Preuninger wrote:
    >>> On Sep 5, 9:06 pm, Arne Vajhøj <> wrote:
    >>>> Chase Preuninger wrote:
    >>>>> I have a bunch of javascript files contained in an xyz.war file on my
    >>>>> JBoss server. I need a way using servlets (not JSP) to load them into
    >>>>> memory using InputStreams, process them and then send them back out to
    >>>>> the client's web browser. My problem is I can't figure out how to
    >>>>> access these files through a servlet I also want to keep it as dynamic
    >>>>> as possible since I don't know where this code may end up.
    >>>> If they are in WEB-INF/classes or WEB-INF/lib then you can use
    >>>> ClassLoader getResourceAsStream to read them.
    >>>> If not then you can try open it as a file via getServletContext
    >>>> getRealPath.
    >>>> Arne
    >>> This is the path returned by the getRealPath("") call.
    >>> C:\Program Files\jboss-4.2.0.GA\server\default\.\tmp\deploy
    >>> \tmp3242525634535.war\
    >>> Can't be found and where did the "." come from.

    >> A handy way of returning the Javascript file is to include it using a
    >> request dispatcher...this eliminates the InputStream and OutputStream
    >> mechanics.
    >>
    >> AHS- Hide quoted text -
    >>
    >> - Show quoted text -

    >
    > Yet again how do I do that and still what does the "." mean?


    You can get a request dispatcher directly from the ServletContext,
    passing in the String path.

    RequestDispatcher getRequestDispatcher(String path)

    From the Javadoc, the path should start with "/", and it's with
    reference to the _current_ context root.

    I wouldn't worry overmuch about the "." in the returned path. The
    "C:\Program Files\jboss-4.2.0.GA\server\default" is a hardwired path for
    the JBoss server you chose (the default configuration); the subpath
    starting with "." is a generated relative path (the ".\" could have been
    left off but no harm done that it's there.) JBoss is simply adding the two.

    AHS
    Arved Sandstrom, Sep 6, 2009
    #6
  7. Chase Preuninger

    Arne Vajhøj Guest

    Chase Preuninger wrote:
    > On Sep 5, 9:06 pm, Arne Vajhøj <> wrote:
    >> Chase Preuninger wrote:
    >>> I have a bunch of javascript files contained in an xyz.war file on my
    >>> JBoss server. I need a way using servlets (not JSP) to load them into
    >>> memory using InputStreams, process them and then send them back out to
    >>> the client's web browser. My problem is I can't figure out how to
    >>> access these files through a servlet I also want to keep it as dynamic
    >>> as possible since I don't know where this code may end up.

    >> If they are in WEB-INF/classes or WEB-INF/lib then you can use
    >> ClassLoader getResourceAsStream to read them.
    >>
    >> If not then you can try open it as a file via getServletContext
    >> getRealPath.

    >
    > This is the path returned by the getRealPath("") call.
    >
    > C:\Program Files\jboss-4.2.0.GA\server\default\.\tmp\deploy
    > \tmp3242525634535.war\
    >
    > Can't be found and where did the "." come from.


    If your JS files are inside the war then they should be
    unpacked in some dir (and the above seems correct if the JS
    files are indeed in the root of the war).

    Arne
    Arne Vajhøj, Sep 8, 2009
    #7
    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. Eric Osman
    Replies:
    4
    Views:
    499
    Wendy S
    Feb 23, 2004
  2. xfan
    Replies:
    5
    Views:
    399
  3. jonesy
    Replies:
    3
    Views:
    957
    jonesy
    Oct 24, 2006
  4. Replies:
    1
    Views:
    305
    Manish Pandit
    Nov 27, 2006
  5. ros
    Replies:
    2
    Views:
    1,423
Loading...

Share This Page