如何使用参数初始化 Java EE 5 JAX-WS 2.0 Web 服务
应用程序配置:
- Web 应用程序使用 java 第一种方法通过注释创建 JAX-WS 2.0 Web 服务。
- WebLogic 10.3
我的要求
我的要求是部署单个 Web 服务实现类,但根据访问服务的 URL 更改逻辑。
问题: 我假设一个好方法是在 web.xml 中部署不同的映射并使用不同的参数初始化它们。有更好的办法吗?
关闭访问 Web 服务的 URL 的逻辑的最佳方法是什么?我应该尝试使用初始化参数在 web.xml 中配置两个 servlet 映射(尝试过,但无法使其工作),还是应该解析服务实现中的 URL?还有其他选择吗?
我尝试过的(但没有成功)
我尝试在
中添加
web.xml 中的元素。但是,无法访问 Web 服务内的 ServletConfig 对象来检索参数。 Web 服务不具备标准 Servlet
的所有功能(即使我实现了 Servlet
或 ServletContextListener
)。我只能访问 WebServiceContext
(看起来),从那里我只能获取
元素 - 但我需要 < init-param>
元素代替。
在 web.xml 中,我使用相同的 Java 类输入两个
元素,但它们映射到两个不同的 URL,如下所示。请注意每个 Servlet 映射中的“source”参数有何不同。
<servlet>
<servlet-name>Foo</servlet-name>
<servlet-class>com.Foo</servlet-class>
<init-param>
<param-name>source</param-name>
<param-value>1</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Foo</servlet-name>
<url-pattern>/Foo</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Bar</servlet-name>
<servlet-class>com.Foo</servlet-class>
<init-param>
<param-name>source</param-name>
<param-value>2</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Bar</servlet-name>
<url-pattern>/Bar</url-pattern>
</servlet-mapping>
Application configuration:
- Web application using java first method of creating JAX-WS 2.0 Web Services with annotations.
- WebLogic 10.3
My Requirements
The requirements I have are to deploy a single web service implementation class, but change logic based on the URL from which the service was accessed.
Question:
I'm assuming a good way to do this is to deploy different mappings in web.xml and initialize them with different parameters. Is there a better way?
What is the best way to switch logic off the URL from which the web service was accessed? Should I try to configure two servlet mappings in web.xml with initialization parameters (tried, but couldn't get it to work), or should I parse the URL in the service impl? Any other alternatives?
What I've Tried (but didn't work)
I have tried adding the <init-param>
in the <servlet>
element in web.xml. However, can't get to the ServletConfig
object inside the web service to retrieve the param. The web service does not have all the functionality of a standard Servlet
(even if I implement Servlet
or ServletContextListener
). I only have access to the WebServiceContext
(it seems) and from there I can only get <context-param>
elements--but I would need <init-param>
elements instead.
In web.xml, I enter two <servlet>
elements using the same Java class, but which map to two different URLs as follows. Notice how the "source" param is different in each Servlet mapping.
<servlet>
<servlet-name>Foo</servlet-name>
<servlet-class>com.Foo</servlet-class>
<init-param>
<param-name>source</param-name>
<param-value>1</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Foo</servlet-name>
<url-pattern>/Foo</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Bar</servlet-name>
<servlet-class>com.Foo</servlet-class>
<init-param>
<param-name>source</param-name>
<param-value>2</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Bar</servlet-name>
<url-pattern>/Bar</url-pattern>
</servlet-mapping>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您很可能已经这样做了,但是您是否尝试在运行时使用
MessageContext
来确定源是什么?我从 http://sirinsevinc.wordpress.com/category/jaxws/ 得到了这个想法。不过还没有尝试过。
You very well may have, but did you try using
MessageContext
at runtime to determine what the source is?I got the idea from http://sirinsevinc.wordpress.com/category/jaxws/. Haven't tried it, though.