【问题标题】:Get context-root from sun-web.xml in web service从 web 服务中的 sun-web.xml 获取 context-root
【发布时间】:2012-05-10 20:30:28
【问题描述】:
在部署时,Web 服务 URL 为 http://localhost:8080/[context-root]/[serviceName]。 context-root 尤其是项目的名称,但除此之外仅显示在 WEB-INF 的 sun-web.xml 中。
如何将这些信息传递给 java 层?我需要以编程方式获取 Web 服务的 url,而 context-root 是目前唯一缺少的信息。
【问题讨论】:
标签:
java
web-services
glassfish
jax-ws
【解决方案1】:
您可以这样做。假设此代码在服务类中
@Resource
WebServiceContext wsContext;
@WebMethod
public String myServiceOperation(){
MessageContext context = wsContext.getMessageContext();
HttpServletRequest res = (HttpServletRequest) context.get(MessageContext.SERVLET_REQUEST) ;
System.out.println(" Context:path "+res.getContextPath());
}
这会将上下文路径打印到控制台。