如何从 JAX-WS Web 服务中访问 ServletContext? [英] How can I access the ServletContext from within a JAX-WS web service?

查看:20
本文介绍了如何从 JAX-WS Web 服务中访问 ServletContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过将对象存储为 servlet 上下文属性来在我的 servlet 和我的 Web 服务 (JAX-WS) 之间共享一个对象.但是如何从 Web 服务中检索 servlet 上下文?

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 service?

推荐答案

JAX-WS 通过消息上下文使 servlet 上下文可用,可以使用 Web 服务上下文检索该上下文.插入以下成员将导致 JAX-WS 将 Web 服务上下文的引用注入到您的 Web 服务中:

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;

然后,您可以使用以下方法访问 servlet 上下文:

Then, you can access the servlet context using:

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

这篇关于如何从 JAX-WS Web 服务中访问 ServletContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆