Can';t让jetty读取jetty-env.xml

Can';t让jetty读取jetty-env.xml,jetty,jndi,Jetty,Jndi,我有以下资料: 启动代码: Server server = new Server(8080); WebAppContext context = new WebAppContext(); context.setDescriptor("/WEB-INF/web.xml"); context.setResourceBase("/home/webapp); context.setContextPath("/"); context.setParentLo

我有以下资料:

启动代码:

    Server server = new Server(8080);

    WebAppContext context = new WebAppContext();

    context.setDescriptor("/WEB-INF/web.xml");
    context.setResourceBase("/home/webapp);
    context.setContextPath("/");
    context.setParentLoaderPriority(true);

    server.setHandler(context);

    server.start();
/home/webapp/WEB-INF中的jetty-env.xml:

 <?xml version="1.0"?> 
 <Configure class="org.mortbay.jetty.webapp.WebAppContext">
   <New id="properties"  class="org.mortbay.jetty.plus.naming.EnvEntry">
     <Arg>property_file</Arg>
     <Arg>/home/webapp/web.properties</Arg>
   </New>  
 </Configure>
初始上下文创建正常,但尝试查找属性_文件键会引发name not found异常。枚举仅返回jndi.properties中定义的键

Context initContext = new InitialContext();
Hashtable<?,?> ht = initContext.getEnvironment();
Enumeration<?> keys = ht.keys();
while(keys.hasMoreElements()) {
    Object key = keys.nextElement();
    System.out.println(key.toString() + "=" + ht.get(key).toString());
}

String props=(String)initContext.lookup("java:comp/env/property_file");
Context initContext=new InitialContext();
Hashtable ht=initContext.getEnvironment();
枚举键=ht.keys();
while(keys.hasMoreElements()){
对象键=keys.nextElement();
System.out.println(key.toString()+“=”+ht.get(key.toString());
}
stringprops=(String)initContext.lookup(“java:comp/env/property_file”);

我做错了什么?

您的jetty版本组合不好


指的是6号码头的使用,还有

java.naming.factory.url.pkgs=org.eclipse.jetty.jndi
java.naming.factory.initial=org.eclipse.jetty.jndi.InitialContextFactory
指向码头9的点使用

这就是把你搞砸的原因。 您正在使用的XML在Jetty 9(仅Jetty 6)上不起作用,您正在定义的属性文件永远不应该为Jetty 9手动定义(它出现在
Jetty jndi-{ver}.jar
中)

9号码头的说明见

这就是使用JNDI支持安装嵌入式Jetty的方法。 您需要在
WebAppContext
配置中启用一些部分,这些部分应该能够使用
jetty env.xml

//启用对web.xml和jetty-env.xml的jndi相关部分的解析
Configuration.ClassList ClassList=Configuration.ClassList.setServerDefault(服务器);
classlist.addAfter(“org.eclipse.jetty.webapp.FragmentConfiguration”,
“org.eclipse.jetty.plus.webapp.EnvConfiguration”,
“org.eclipse.jetty.plus.webapp.PlusConfiguration”);
最后,这里记录了jetty env.xml的结构和语法

所以你的例子应该是这样的



最后一条建议,不要在您的项目中使用
jetty all.jar
,它的存在只是为了允许对jetty进行一些基本的命令行实验,它不是用来作为项目中的依赖项。

对不起,我对jetty不熟悉,我忘了提到我使用的是嵌入式模式。 现在我已经知道,在嵌入式模式下,jetty不会自动读取jetty-env.xml。您必须明确地执行此操作,例如:

    Resource jettyEnv = Resource.newSystemResource("jetty-env.xml");
    XmlConfiguration conf = new XmlConfiguration(jettyEnv.getInputStream());
    Object obj = conf.configure();
    WebAppContext context = (WebAppContext)obj;

关于混合不同jetty版本的回答仍然适用。

你真的使用了
WEB\u INF
而不是
WEB-INF
?不,它实际上是WEB-INF。很抱歉,输入错误。这另一个可能会有帮助。@FrédéricDumont这个问题太老了,不再相关,它使用jetty 6概念(Jetty 6早在2010年就结束了)这是错误的,完全忽略了jndi和servlet规范所需的webapp类加载器、线程上下文、线程作用域和jndi作用域。正确设置
配置.ClassList
,以便WebAppContext初始化可以正确加载Jetty-env.xml(参见上面的答案)该链接仍然没有告诉我如何让jetty读取jetty-env.xml。它只告诉我如何以编程方式设置jndi值。如果我读取jetty-env.xml的方式是错误的,那么我仍然不知道哪种方式是正确的。它是
配置.ClassList
的一部分,如文档中所述(文档示例中的第18行至第20行),以及上面的答案。
EnvConfiguration
告诉jetty加载
jetty env.xml
。在您的任何问题中,您粘贴的示例都没有显示这一点。我在您的各种问题的注释和答案中多次提到
Configuration.ClassList
。这是关键。我真的不知道任何w我想我终于明白了…Configuration.ClassList实际上会导致WEB-INF/jetty-env.xml被读取(在上下文初始化期间),只要它存在。我首先解释它的方式是,它只会“启用”解析,但您仍然必须显式加载它。
$ jar -tvf lib/jetty-jndi-9.2.9.v20150224.jar | grep jndi.properties
125 Tue Feb 24 10:49:42 MST 2015 jndi.properties
    Resource jettyEnv = Resource.newSystemResource("jetty-env.xml");
    XmlConfiguration conf = new XmlConfiguration(jettyEnv.getInputStream());
    Object obj = conf.configure();
    WebAppContext context = (WebAppContext)obj;