简体   繁体   English

Java,NetBean:从Web Service方法访问web.xml上下文参数?

[英]Java, NetBean : Access web.xml context parameters from Web Service method?

I am new to java so excuse my lame questions:) 我是java的新手,请原谅我的蹩脚问题:)

I am trying to build a web service in Java NetBeans 6.1 , but I have some troubles with configuration parameters ( like .settings in .net). 我正在尝试在Java NetBeans 6.1中构建Web服务,但是我在配置参数方面遇到了一些麻烦(比如.net中的.settings)。

What is the right way to save and access such settings in a java web service. 在Java Web服务中保存和访问此类设置的正确方法是什么。

Is there a way to read context parameters from web.xml in a web method? 有没有办法在Web方法中从web.xml读取上下文参数?

If no what are the alternatives for storing your configuration variables like pathnames ? 如果没有什么是存储配置变量(如路径名)的替代方法?

Thank you 谢谢

Is there a way to read context parameters from web.xml in a web method? 有没有办法在Web方法中从web.xml读取上下文参数?

No, this is not easily done using the out-of-the-box. 不,使用开箱即用不容易做到这一点。 The Web Service system (JAX-WS) has minimal awareness of the Servlet engine (Tomcat). Web服务系统(JAX-WS)对Servlet引擎(Tomcat)的了解很少。 They are designed to be isolated. 它们被设计为孤立的。

If you wanted to use the context parameters, your web service class would need to implement ServletContextListener and retrieve the desired parameters in the initialization parameter (or save the context for later use). 如果要使用上下文参数,则Web服务类需要实现ServletContextListener并在初始化参数中检索所需的参数(或保存上下文以供以后使用)。 Since the Servlet engine and JAX-WS would each have different instances of the object, you'd need to save the values to a static member. 由于Servlet引擎和JAX-WS每个都有不同的对象实例,因此您需要将值保存到静态成员。

As Lars mentioned, the Properties API or JNDI are your best bets as they're included with Java and are fairly well-known ways to retrieve options. 正如Lars所提到的,Properties API或JNDI是您最好的赌注,因为它们包含在Java中,并且是众所周知的检索选项的方法。 Use Classloader.getResource() to retrieve the Properties in a web context. 使用Classloader.getResource()检索Web上下文中的Properties。

If you are using servlets, you can configure parameters in web.xml: 如果您使用的是servlet,则可以在web.xml中配置参数:

<servlet>
  <servlet-name>jsp</servlet-name>
  <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
    <init-param>
      <param-name>fork</param-name>
      <param-value>false</param-value>
     </init-param>
</servlet>

These properties will be passed in a ServletConfig object to your servlet's "init" method. 这些属性将在ServletConfig对象中传递给servlet的“init”方法。

Another way is to read your system's environment variables with 另一种方法是使用读取系统的环境变量

System.getProperty(String name);

But this is not recommended for other than small programs and tests. 但是除了小程序和测试之外,不建议这样做。

There is also the Properties API if you want to use ".properties" files. 如果要使用“.properties”文件,还有Properties API。 http://java.sun.com/javase/6/docs/api/java/util/Properties.html http://java.sun.com/javase/6/docs/api/java/util/Properties.html

Finally, I believe looking up configurations with JNDI is pretty common when developing modern web service applications, Netbeans and app containers have pretty good support for that. 最后,我认为在开发现代Web服务应用程序时,使用JNDI查找配置非常常见,Netbeans和app容器对此有很好的支持。 Google it. 谷歌一下。

MessageContext ctx = MessageContext.getCurrentThreadsContext();       
Servlet wsServlet = (Servlet) ctx.getProperty(HTTPConstants.MC_HTTP_SERVLET);         
ServletConfig wsServletConfig = wsServlet.getServletConfig();                 
ServletContext wsContext = wsServletConfig.getServletContext();   

I think the correct answer is ... as always ... "It depends". 我认为正确答案是......一如既往......“这取决于”。 If you are just running a small implementation with a single server then it depend much on the WS technology you want to use. 如果您只是使用单个服务器运行一个小实现,那么它很大程度上取决于您要使用的WS技术。 Some make the servlet context and the context-params easy to access, others don't, in which case accessing properties from a properties file may be easier. 有些使servlet上下文和context-params易于访问,而有些则无法访问,在这种情况下,从属性文件访问属性可能更容易。 Are you going to have an array of servers in a load balanced environment with high traffic where updating the setting for all servers must be instant and centralized in-case of fail-over? 您是否要在负载平衡的环境中拥有一系列服务器,并且流量较高,在更新所有服务器的设置时必须立即进行集中处理,以防故障转移? If that's the case then do you really want to update the config files for all servers in the farm? 如果是这种情况那么你真的想更新服务器场中所有服务器的配置文件吗? How do you synchronize those changes to all those servers? 如何将这些更改同步到所有这些服务器? Does it matter to you? 对你有用吗? If you're storing path-names in a config file then you probably intend to be able to update the path-names to another host in case certain host goes down ("\\file_server_host\\doc_store" --> "\\backup_file_server_host\\doc_store") in which case is may actually be better to fail-over using DNS instead. 如果您将路径名存储在配置文件中,那么您可能希望能够在某些主机出现故障时将路径名更新到另一台主机(“\\ file_server_host \\ doc_store” - >“\\ backup_file_server_host \\ doc_store” )在这种情况下,实际上可能更好地使用DNS进行故障转移。 There are too many variables. 变量太多了。 It really depends on the design; 这真的取决于设计; needs; 需要; scale of the app. 应用程序的规模。


For simplicity sake, if you just want a simple equivalent of a .settings file then you want a .properties file. 为简单起见,如果您只想要一个简单的.settings文件,那么您需要一个.properties文件。 Here is an example where I have recently used this in a project: https://github.com/sylnsr/docx4j-ws/blob/master/src/docx4j/TextSubstitution.java 这是我最近在项目中使用它的示例: https//github.com/sylnsr/docx4j-ws/blob/master/src/docx4j/TextSubstitution.java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM