【发布时间】:2014-02-10 03:19:06
【问题描述】:
我有一个 JSF Web 应用程序,我想使用嵌入的 tomcat 运行它。到目前为止它正在工作[包括JDBCRealm,在以下代码sn-p中的context.xml中指定],除了登录后我的代码无法实际获取源中指定的连接资源,抛出NoInitialContextException。
我可能遗漏了一些明显的东西,但是关于嵌入 tomcat 的流量似乎很少。 [Tomcat7.0.47, JDK7].
作为per 其他questions 上this site 在此站点上,我已经尝试了许多添加初始环境的变体,但我无法弄清楚这是否真的是我的问题,或者我只是还没有找到这个 tomcat 服务器的正确咒语。
Tomcat 启动代码:
tomcat = new Tomcat();
tomcat.setPort(port);
tomcat.setBaseDir(".");
Context ctx = tomcat.addWebapp("/" + appname, appname);
// The login realm specified in this XML file is a JDBC realm, and the server correctly logs users in, so I believe this is parsed.
ctx.setConfigFile(new URL("file:///home/chunky/src/aqmt/AQMTEmbed/webapps/AQMTWeb/META-INF/context.xml"));
ContextResource resource = new ContextResource();
resource.setName("jdbc/aqmtwebdb");
resource.setAuth("Container");
resource.setType("javax.sql.DataSource");
resource.setScope("Sharable");
resource.setProperty("driverClassName", "com.mysql.jdbc.Driver");
resource.setProperty("url", "jdbc:mysql://localhost:3306/aqmtweb?autoreconnect=true");
resource.setProperty("username", "username");
resource.setProperty("password", "password");
ctx.getNamingResources().addResource(resource);
tomcat.start();
在 web 应用程序本身是这样的:
InitialContext ctx = new InitialContext();
// This line here throws the exception:
DataSource webds = (DataSource)ctx.lookup("java:/comp/env/jdbc/aqmtwebdb");
抛出的异常是:
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:344)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
at org.rand.aqmt.UserBean.<init>(UserBean.java:77)
[其中行 UserBean.java:77 是上面的 ctx.lookup()]
我不知道如何进步。
【问题讨论】: