I've got a serious problem that I can't figure out. This should be *really*
simple - but it's alluding me. I have an ear file with a number of servlets
inside it. This ear file also contains a resource that my servlets want to
read from. This resource is located (within the ear file) at:
"/content/structure/a.txt". The file contains raw ascii.
To have my serlvet read from this file, I thought it would be as simple as:
URL content =
servlet.getServletContext().getResource("/content/structure/a.txt");
// pw is an instance of PrintWriter
pw.print(content.getContent());
However, when I execute the above I get the following exception:
java.lang.NullPointerException
at
java.net.URLConnection.stripOffParameters(URLConnection.java:1011)
at java.net.URLConnection.getContentHandler(URLConnection.java:979)
at java.net.URLConnection.getContent(URLConnection.java:581)
OK, so fine - I figure that I'm new to this. The documentation for the URL
class tells me there is another way around this. It says that rather then
calling getContent() directly, I can open a stream on the URL and read from
that, like so:
URL content =
servlet.getServletContext().getResource("/content/structure/scenario2resourc
es.txt");
FileInputStream fis = content.openStream();
but when I do this I get the following different exception:
java.lang.ClassCastException: weblogic.utils.zip.SafeZipFileInputStream^M
at Utility.writeFileToOutput(Utility.java:134)^M
Apparently this thing is trying to return a web-logic-specific stream (which
is incompatible with an FileInputStream) - which I don't want to use or
learn about since I want my stuff to work under other webservers.
Can anyone tell me what I'm doing wrong? This should be simple.
-john
--
John Hilgedick
WisdomTools, Inc.
john.hi...@wisdomtools.com
The ClassCastException is because that content.openStream() is not FileInputStream
and has nothing to do with Weblogic. You should treat it as normal InputStream.
I appreciate your response. I tried treating it as a normal InputStream -
but it didn't make any difference. Here's what I did:
URL content =
servlet.getServletContext().getResource("/content/structure/scenario2resourc
es.txt");
InputStream is = (InputStream)content.getContent();
And here's the exception:
java.lang.NullPointerException^M
at
java.net.URLConnection.stripOffParameters(URLConnection.java:1011)^M
at
java.net.URLConnection.getContentHandler(URLConnection.java:979)^M
at java.net.URLConnection.getContent(URLConnection.java:554)^M
at java.net.URL.getContent(URL.java:807)^M
at Utility.writeFileToOutput(Utility.java:134)^M
You can see that it is definitely breaking somewhere in getContent().
If you have any other ideas, I'd appreciate hearing about them.
-john
Does
InputStream in =
servlet.getServletContext().getResourceAsStream("/content/structure/a.txt");
work ?
> -john
--
Dimitri
Muchas gracias!
-john
"Dimitri I. Rakitine" <dim...@dima.dhs.org> wrote in message
news:3ddd...@newsgroups.bea.com...