Atmocreations
Atmocreations

Reputation: 10061

Eclipse Web Service: how to get current path

Sure this question has been asked some times already. Still, I don't get the right answer until now.

Using Eclipse, I did the following steps:

  1. Create Dynamic Web Project
  2. Implement my service class
  3. Create new Web Service

It all works and the service more or less starts. Apparently, a WSDL (which I will use later) is generated as well.

In the constructor of my service implementation, I'm desperately trying to read a custom file - which resides in WebContent

I'm not very experienced at creating web services. But I'm using Tomcat 7, Axis 2 and some JPA. In any case, I cannot access the ServletContext class. I'm not implementing my own servlet either.

I tried all of these:

1

`new File(".").getAbsolutePath()`

--> returns my own home directory.

2

@Resource
private WebServiceContext context;
....
ServletContext servletContext =
(ServletContext) context.getMessageContext().get(MessageContext.SERVLET_CONTEXT);

--> context is always null. I found somewhere, that this is only injected after calling a first service method. Not really my solution.

3

Don't have the code handy anymore, I used some snippet with NIO. Gotta find it... Didn't work either.

Upvotes: 0

Views: 621

Answers (1)

malintha
malintha

Reputation: 176

If you are trying to read a file in the service, you can place it in the package and access as a resource.

InputStream is = className.class.getResourceAsStream("filename.xml");

Upvotes: 1

Related Questions