Accessing Server File with Servlets

  • Thread starter Chase Preuninger
  • Start date
C

Chase Preuninger

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.
 
A

Arne Vajhøj

Chase said:
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
 
C

Chase Preuninger

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

Arved Sandstrom

Chase said:
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
 
C

Chase Preuninger

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?
 
A

Arved Sandstrom

Chase said:
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
 
A

Arne Vajhøj

Chase said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,765
Messages
2,569,568
Members
45,042
Latest member
icassiem

Latest Threads

Top