• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to read config file when web service starts up

 
Ranch Hand
Posts: 165
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
user a listener, ServletContextListener and register it in your web.xml file.
 
Ravi Danum
Ranch Hand
Posts: 165
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
since its a (servlet-based) i would say yes.
 
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 165
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 165
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 165
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Peer that makes sense.

-Ravi
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ravi,

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

Bibhu
 
reply
    Bookmark Topic Watch Topic
  • New Topic