当今的企业级开发中J2EE技术无疑是运用的最广泛的一种。在架设服务器的时候通常出现Web容器和应用服务器不在同一台主机的问题,这时我们可以通过配置Web容器中的web.xml文件来实现两段的联系。
在web.xml中我们添加入如下语句:
<!-- weblogic url -->
<env-entry>
<description>url of Context parameter of invoke ejb</description>
<env-entry-name>param/url</env-entry-name>
<env-entry-value>t3://127.0.0.1:7001</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>
我们按照该格式只要把地址设置好即可。
然后在编写一个获取该信息的工具类,在其中入如下代码:
private InitialContext getInitialContext()
throws Exception
{
String s;
Object obj;
Object obj1;
InitialContext initialcontext = new InitialContext();
s = (String)initialcontext.lookup("java:comp/env/param/url");
obj = null;
obj1 = null;
Object obj2 = null;
Properties properties;
properties = new Properties();
properties.put("java.naming.factory.initial", "weblogic.jndi.WLInitialContextFactory");
properties.put("java.naming.provider.url", s);
if(obj != null)
{
properties.put("java.naming.security.principal", obj);
properties.put("java.naming.security.credentials", obj1 != null ? ((Object) (obj1)) : "");
}
return new InitialContext(properties);
}
这里我们是已Weblogic为列,这样我们就能调到另一台机器上应用服务器的JNDI了
是不是很简单