How can I access the ServletContext from within a JAX-WS web service?

后端 未结 2 656
遥遥无期
遥遥无期 2020-12-02 09:06

I want to share an object between my servlets and my webservice (JAX-WS) by storing it as a servlet context attribute. But how can I retrieve the servlet context from a web

2条回答
  •  一生所求
    2020-12-02 09:54

    The servlet context is made available by JAX-WS via the message context, which can be retrieved using the web service context. Inserting the following member will cause JAX-WS to inject a reference to the web service context into your web service:

    import javax.annotation.Resource;
    import javax.servlet.ServletContext;
    import javax.xml.ws.WebServiceContext;
    import javax.xml.ws.handler.MessageContext;
    
    ...
    
    @Resource
    private WebServiceContext context;
    

    Then, you can access the servlet context using:

    ServletContext servletContext =
        (ServletContext) context.getMessageContext().get(MessageContext.SERVLET_CONTEXT);
    

提交回复
热议问题