Read XML file on server using ServletContext.getResource() never works for me

415 views
Skip to first unread message

Lucas86

unread,
Feb 10, 2010, 12:09:27 AM2/10/10
to Google Web Toolkit
I've been trying to read an XML in my RPC servlet and I'm having
trouble reading the file in development mode using
ServletContext.getResource(). This is my first try, and I think I must
be missing something simple but I haven't been able to find what the
missing piece is. Every path I've passed to getResource has returned
null, but when I call getResourcePaths() on the same context I get a
full list of expected paths. When I any of these paths to getResource,
it still returns null.

==Not complete code, but just copied from my running project==
ServletContext context = getServletContext();
String paths = context.getResourcePaths("/
myapp/").toString(); //Returns a list, including "/myapp/
hosted.html"
java.net.URL testUrl = context.getResource("/myapp/hosted.html"); //
Returns null (Not really what I'm looking for, but I need to get it
finding something first)
====

I realize this isn't strictly a GWT question, but that's where I'm
working and I've heard so many unhelpful not-quite-related solutions
(that usually just say "use getResource") that I really wanted to ask
here in case there are any special cases when working with the
imbedded jetty server in GWT 2.0. Does anyone know why
getResourcePaths sees what I'm looking for but the same paths fail
when passed to getResource?

Thanks for any help.

Joe Cole

unread,
Feb 10, 2010, 12:32:31 AM2/10/10
to Google Web Toolkit
From memory we had heaps of trouble using getResource, and ended up
using getResourceAsStream. Give that a go?

InputStream stream =
configuration.getServletContext().getResourceAsStream(location);

Lucas86

unread,
Feb 10, 2010, 4:14:58 PM2/10/10
to Google Web Toolkit
Unfortunately that doesn't get results that are any better. What's the
configuration that you're referring to? I just use the following

public class DataServiceImpl extends RemoteServiceServlet implements
DataService {
public String getData() {
ServletContext context = getServletContext();
ServletContext context = getServletContext();
InputStream t1, t2, t3, t4, t5, t6, t7;
InputStream isr;
InputStream stream;
String paths = "Dummy start value";
try {
//This is lifted straight from the google groups answer, what is
the configuration?
//Tried on 2/10, all of these get null too
stream = getServletContext().getResourceAsStream("/");
paths = context.getResourcePaths("/").toString(); //Gets: /, /
MyApp.css, /MyApp.html, /WEB-INF/...
isr = context.getResourceAsStream("/");
t1 = context.getResourceAsStream("/MyApp.css");
t2 = context.getResourceAsStream("/MyApp.html");
t3 = context.getResourceAsStream("MyApp.css");
t4 = context.getResourceAsStream("MyApp.html");
t5 = context.getResourceAsStream("/WEB-INF/");
t6 = context.getResourceAsStream("WEB-INF/");
t7 = context.getResourceAsStream("WEB-INF");
} catch (Exception e) {
System.out.println("Problem!");
}
}
}

Am I doing something wrong in how I get the context? I could
understand if I was doing something totally wrong, but again, the
getPaths call lists exactly what I expect. I'm also working on a Mac,
if anyone's heard of that making any difference. Thanks again.

On Feb 10, 12:32 am, Joe Cole <profilercorporat...@gmail.com> wrote:
> From memory we had heaps of trouble using getResource, and ended up
> using getResourceAsStream. Give that a go?
>
> InputStream stream =
> configuration.getServletContext().getResourceAsStream(location);
>
> On Feb 10, 6:09 pm, Lucas86 <lucaslo...@gmail.com> wrote:
>
>
>

> > I've been trying toreadanXMLin my RPC servlet and I'm having

Reply all
Reply to author
Forward
0 new messages