5
我有一個我想使用嵌入式tomcat運行的JSF Web應用程序。到目前爲止,它的工作[包括JDBCRealm,在下面的代碼片段中的context.xml中指定],除了登錄後,我的代碼無法實際獲取源中指定的連接資源,拋出NoInitialContextException。嘗試獲取數據源時的Tomcat嵌入式:NoInitialContextException
我可能錯過了一些顯而易見的事情,但是似乎有關於嵌入式tomcat的流量很少。 [Tomcat7.0.47,JDK7]。
由於per上this site本網站其他questions,我已經嘗試了多種變體上添加一個初始環境,但我一直無法弄清楚,如果這真的是我的問題,還是我只是還沒有爲這個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()]
我對如何進步感到相當茫然。
真棒,謝謝! – icyitscold
+ 100。對於那個愚蠢的事情,我只失去了一天半的時間,如果我沒有碰到你的帖子,我可能已經失去了一生......謝謝你。 –