• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to read config file when web service starts up

 
Ravi Danum
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

How can a web service read from a config file at its startup? I know in servlets, the code would get called from the servlet's init method.

I thought of writing a handler, but handlers get called for each request and response.

I am using a servlet based web service (not EJB based ws) and JAX-WS.

Thanks for your help.

Ravi


 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
user a listener, ServletContextListener and register it in your web.xml file.
 
Ravi Danum
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Omar,

I would use the ServletContextListener with a servlet, but is that the recommended way with a (servlet-based) webservice?

Thanks for your response.

Ravi
 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
since its a (servlet-based) i would say yes.
 
Peer Reynders
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably something along the lines of:



Hello.java
javax.annotation.Resource
javax.xml.ws.WebServiceContext
A little bit about Message Context in JAX-WS

 
Ravi Danum
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Peer, Omar,

Thanks so much. This is perfect. I am so grateful for this information.

-Ravi
 
Ravi Danum
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peer,

I have tried the code:

...
import javax.xml.ws.WebServiceContext;
import javax.annotation.Resource;
import javax.servlet.ServletContext;

@WebService
public class Hello {

...
@WebMethod(exclude = true)
@Resource
public void initializeContext(WebServiceContext wsContext) {
MessageContext mc = wsContext.getMessageContext();
ServletContext context = ((javax.servlet.ServletContext) mc.get(MessageContext.SERVLET_CONTEXT));
String paramValue = context.getInitParameter("ParamName");

...
}
...
}

and I can't call: wsContext.getMessageContext() unless it is called from within an exposed web service method.
From Glassfish, I get this error:

java.lang.IllegalStateException: getMessageContext() can only be called while servicing a request

Do you know of another way to read some parameters (hopefully servlet initialization params from web.xml) from the initializeContext method?

Many thanks for your attention.

Ravi
 
Peer Reynders
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any chance you can use lazy initialization? Have every web method call the lazy initialization method with the MessageContext but only initialize on the first invocation.
 
Ravi Danum
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Peer that makes sense.

-Ravi
 
Bibhu Biswal
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ravi,

I am new to web service, Could you please show the code snippet.

Bibhu
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic