简体   繁体   中英

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.

如果您尝试读取服务中的文件,则可以将其放入包中并作为资源进行访问。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM