【发布时间】:2016-02-08 15:39:46
【问题描述】:
我正在为班级构建一个 Web 应用程序(是的,这是一项家庭作业)。我正在使用 Eclipse 和 Tomcat 8。我有以下服务工厂代码:
package com.OrderOnline.model.service.factory;
import javax.naming.Context;
import javax.naming.InitialContext;
import com.OrderOnline.model.service.interfaces.IService;
public class ServiceFactory
{
public IService getService(String name) throws Exception
{
try
{
Class<?> newClass = Class.forName(getImplName(name));
return (IService)newClass.newInstance();
}
catch(Exception e)
{
throw new Exception(name);
}
}
private String getImplName(String name) throws Exception
{
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
String lookupClass = (String) envCtx.lookup(name);
return lookupClass;
}
}
我希望这会在我位于 OrderOnline/WebContent/WEB-INF/web.xml 的 web.xml 文件中查看以下内容:
<env-entry>
<env-entry-name>IUserSvc</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>com.OrderOnline.model.service.interfaces.IUserSvc</env-entry-value>
</env-entry>
相反,我在运行此行时抛出异常:
Context envCtx = (Context) initCtx.lookup("java:comp/env");
例外是:
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
我对此很陌生,所以我可能会遗漏一些重要的信息来帮助我,我很乐意提供更多需要的信息。感谢任何可以帮助我从 web.xml 文件中读取 env-entry-value 的帮助。
【问题讨论】: